Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#&;非矩阵数据类型的MATLAB互操作性_C#_Matlab_Interop_Matlab Deployment - Fatal编程技术网

C#&;非矩阵数据类型的MATLAB互操作性

C#&;非矩阵数据类型的MATLAB互操作性,c#,matlab,interop,matlab-deployment,C#,Matlab,Interop,Matlab Deployment,我正在编写一个C程序,需要调用MATLAB处理例程。我一直在看MATLAB的COM接口。不幸的是,COM接口在可交换的数据类型方面似乎相当有限。支持矩阵和字符数组,但似乎不支持使用COM接口在C#和MATLAB之间交换结构数据或单元数组。例如,在以下代码中(假设名为IM000000的DICOM图像存在于适当的文件夹中),MATLAB变量“img”和“header”分别是256x256 int16矩阵和结构。GetWorkspaceData调用对于“img”工作正常,但是对于“header”返回n

我正在编写一个C程序,需要调用MATLAB处理例程。我一直在看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是否有合适的解决方法?如果没有,MATLAB Builder NE附加组件是否很好地支持此功能?

请考虑查看(MATLAB引擎API的包装器)。然后可以像这样交换结构:

var engine = Engine.Open(false);    
var array = MxArray.CreateStruct();

array.SetField("MyField1", "toto");
array.SetField("MyField2", 12.67);
engine.SetVariable("val", array);
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

注意:这个LGPL包装器不是我的,请查看它的API以了解更多详细信息。

我最终使用MatlabBuilder NE附加组件解决了这个问题。代码最终看起来像这样:

var engine = Engine.Open(false);    
var array = MxArray.CreateStruct();

array.SetField("MyField1", "toto");
array.SetField("MyField2", 12.67);
engine.SetVariable("val", array);
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

考虑使用C++和CLI。与.NET托管代码(包括C#)和Matlab原生MEXAPI的无缝互操作性。