如何在C#中获取MATLAB函数返回的矩阵值

时间:2017-05-06 12:09:34

标签: c# matlab

我试图在C#中调用MATLAB函数。我成功运行了一个返回一个值的简单示例,但我想返回一个向量和一个矩阵。我怎么能这样做?

// Create the MATLAB instance 
MLApp.MLApp matlab = new MLApp.MLApp();

// Change to the directory where the function is located 
matlab.Execute(@"cd c:\MATLABcodevisualstudio");

// Define the output 
object result = null;

// Call the MATLAB function myfunc
matlab.Feval("test", 2 , out result, 3.14, 42.0,"world");

// double[,] arr = (double[,])((MWNumericArray)result).ToArray(MWArrayComponent.Real); // if i use this it returns error 

// Display result 
object[] res = result as object[];

现在结果在object resultdouble[,])中返回。我想从这个double数组获取值并将它们用于其他计算并将它们写入.csv文件。但是,当我尝试

 double[,] arr = (double[,])((MWNumericArray)result).ToArray(MWArrayComponent.Real);

它出错了

Additional information: Unable to cast object of type
'System.Object[]' to type
'MathWorks.MATLAB.NET.Arrays.MWNumericArray'.

如何访问并返回返回对象中的值?

0 个答案:

没有答案