我试图在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 result
(double[,]
)中返回。我想从这个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'.
如何访问并返回返回对象中的值?