C# & MATLAB interoperability for non-matrix datatypes

好久不见. 提交于 2019-12-05 12:29:05

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
CitizenInsane

Consider having a look to LabSharp (a wrapper around the Matlab engine API). You can then exchange structure like this:

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

array.SetField("MyField1", "toto");
array.SetField("MyField2", 12.67);
engine.SetVariable("val", array);

NB: This LGPL wrapper is not mine, please have a look to its API for more details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!