Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 非矩阵数据类型的C#和MATLAB互操作性

我正在编写一个需要调用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

更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 22:40


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.