如何在已编译的.NET程序集中使用Matlab对象? [英] How can I use Matlab objects in compiled .NET assemblies?
问题描述
我有一个基本的Matlab类,我想在C#中实例化.
I have a basic Matlab class which I want to instantiate in C#.
classdef MyClass
properties
Value
end
methods
function obj=MyClass(v)
obj.Value = v;
end
function display(obj)
disp(obj.Value);
end
end
end
然后将其内置到.DLL文件中,并与关联的Matlab命名空间(MathWorks.MATLAB.NET.Arrays,MathWorks.MATLAB.NET.Utility)一起导入C#项目中.
This is then built into a .DLL file and imported in a C# project along with the associated Matlab namespaces (MathWorks.MATLAB.NET.Arrays, MathWorks.MATLAB.NET.Utility).
在C#端,我试图以此构建此类的实例:
On the C# side, I am trying to build an instantiation of this class thus:
Untitled2.MLTestClass matlab = new Untitled2.MLTestClass();
MWCharArray input = new MWCharArray("Initial");
MWArray[] result = matlab.MyClass(1, input);
在最后一行代码的末尾,result.Length = 1并且result [0] = null.我以某种方式期望以某种方式获得对新创建的Matlab对象的引用.我在想,这有可能吗?如果是的话,那该如何完成呢?如果没有,有没有办法解决? (我基本上有一个用C#编写的GUI组件,我不想将其集成到Matlab中,反之则相反).
By the end of the last line of code, result.Length = 1 and result[0] = null. I was somehow expecting to obtain the reference to the newly created Matlab object somehow. I was wondering, is this even possible? And if yes, then how can this be accomplished? If no, is there a way around it? (I basically have a GUI component written in C# which I don't want to integrate in Matlab, but rather, the other way round).
推荐答案
It is not possible to use Matlab classes inside .NET assemblies. There are numerous workarounds:
- 将变量定义为 global ,并使用几个包装其方法的函数来访问它
- 将您的Matlab类返回为struct中字段的值.
- Define your variable as global , and access it with several functions that wrap its methods
- Return your Matlab class as a value of field in struct.
以下是(1)的代码段:
Here is a code snippet for (1):
function CreateMyClass(st)
global myClass;
myClass = MyClass(st);
end
function DisplayMyClass()
global myClass;
myClass.display();
end
这篇关于如何在已编译的.NET程序集中使用Matlab对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!