C#&放大器;用于非矩阵数据类型的MATLAB互操作性 [英] C# & MATLAB interoperability for non-matrix datatypes

查看:197
本文介绍了C#&放大器;用于非矩阵数据类型的MATLAB互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写需要调用MATLAB处理程序C#程序。我一直在寻找MATLAB的COM接口。不幸的是,COM接口出现在可以交换数据的类型的角度来相当有限。矩阵和字符数组的支持,但似乎并没有被使用COM接口,C#和MATLAB之间交换的数据结构或单元阵列的支持。例如,在下面的代码(假设名为IM000000一个DICOM影像出现在相应的文件夹),在MATLAB变量IMG和头是​​一个256×256 INT16矩阵和结构,分别为。该GetWorkspaceData调用工作正常,IMG,而是返回null'头',因为'头'是一个结构。

I'm writing a C# program that needs to call MATLAB processing routines. I've been looking at MATLAB's COM interface. Unfortunately, the COM interface appears to be rather limited in terms of the types of data that can be exchanged. Matrices and character arrays are supported, but there doesn't seem to be support for exchanging struct data or cell arrays between C# and MATLAB using the COM interface. For example, in the following code (assuming that a DICOM image named IM000000 is present in the appropriate file folder), the MATLAB variables 'img' and 'header' are a 256x256 int16 matrix and a struct, respectively. The GetWorkspaceData call works fine for 'img', but returns null for 'header' because 'header' is a struct.

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生成器支持NE附加?

Is there a suitable workaround for marshalling struct data to/from MATLAB using the COM interface? If not, is this functionality well supported by the MATLAB Builder NE add-on?

推荐答案

我结束了使用MATLAB生成器NE附加来解决这个问题。该代码最终看起来是这样的:

I ended up using the MATLAB Builder NE add-on to solve the problem. The code ends up looking something like this:

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#&放大器;用于非矩阵数据类型的MATLAB互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆