我试图将3D数组从C#传递到MATLAB函数中。这是我的代码:
// here's the code in C#
MLApp.MLApp matlab = new MLApp.MLApp();
//load function
matlab.Execute(@"cd C:\Users\1303092.local\Matlab\EmGm");
//initialized input and output
int[,,] data1 = new int[2,3,5]{{{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}},{{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}}};
object result1 = null;
object result2 = null;
//use function with the data from C#
matlab.Feval("myfunc", 2, out result1, data1);
//type casting
object[] res1 = result1 as object[];
double[, ,] data2 = res1[1] as double[, ,];
//use function with the casted data from MATLAB
matlab.Feval("myfunc", 2, out result2, data2);
object[] res2 = result2 as object[];
// here's the MATLAB function
function [x,y] = myfunc(a)
x = a;
y = ones(2,3,5);
end
我在代码末尾插入了一个断点来检查变量中的值,如下所示:
看起来C#在获取返回值时没有任何困难,因为它确实识别了返回的参数“y”。但是,当MATLAB从C#获取数组时,可能会出现一些类型转换问题,因为返回的参数“x”在C#中始终被识别为NULL。
我也以完全相同的方式尝试过二维数组,一切运行良好。错误是否与此处的维度相关?任何想法的人?