Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我正在编写一个需要调用MATLAB处理例程的C#程序。我一直在看MATLAB的COM接口。不幸的是,就可以交换的数据类型而言,COM接口似乎受到很大限制。支持矩阵和字符数组,但似乎不支持使用COM接口在C#和MATLAB之间交换结构数据或单元格数组。例如,在以下代码中(假设在相应的文件夹中存在一个名为IM000000的DICOM图像),MATLAB变量'img'和'header'分别为256x256 int16矩阵和结构。 GetWorkspaceData调用对于'img'正常工作,但对于'header'返回null,因为'header'是一个结构。
public class MatlabDataBridge { MLApp.MLAppClass matlab; public MatlabDataBridge() { matlab = new MLApp.MLAppClass(); } public void ExchangeData() { matlab.Execute(@"cd 'F:\Research Data\'"); matlab.Execute(@"img = dicomread('IM000000');"); matlab.Execute(@"header = dicominfo('IM000000');"); matlab.GetWorkspaceData(@"img", "base", out theImg); // correctly returns a 2D array matlab.GetWorkspaceData(@"header", "base", out theHeader); // fails, theHeader is still null } } 是否存在使用COM接口将结构数据编入MATLAB或从MATLAB编入结构数据的合适解决方法?如果没有,MATLAB Builder NE插件是否很好地支持此功能? 回答: 我最终使用了MATLAB Builder NE插件来解决该问题。该代码最终看起来像这样: using MathWorks.MATLAB.NET.Arrays; using MathWorks.MATLAB.NET.Utility; using MyCompiledMatlabPackage; // wrapper class named MyMatlabWrapper is here ... matlab = new MyMatlabWrapper(); MWStructArray foo = new MWStructArray(1, 1, new string[] { "field1", "field2" }); foo["field1", 1] = "some data"; foo["field2", 1] = 5.7389; MWCellArray bar = new MWCellArray(1, 3); bar[1, 1] = foo; bar[1, 2] = "The quick brown fox jumped over the lazy dog."; bar[1, 3] = 7.9; MWArray result[]; result = matlab.MyFunction(foo, bar); // Test the result to figure out what kind of data it is and then cast // it to the appropriate MWArray subclass to extract and use the data 更多&回答... |
![]() |
![]() |