How can I use Matlab objects in compiled .NET assemblies?

醉酒当歌 提交于 2019-12-06 06:33:04

问题


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

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).

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);

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).


回答1:


It is not possible to use Matlab classes inside .NET assemblies. There are numerous workarounds:

  1. Define your variable as global , and access it with several functions that wrap its methods
  2. Return your Matlab class as a value of field in struct.

Here is a code snippet for (1):

function CreateMyClass(st)
    global myClass;
    myClass = MyClass(st);
end

function DisplayMyClass()
    global myClass;
    myClass.display();
end


来源:https://stackoverflow.com/questions/8841146/how-can-i-use-matlab-objects-in-compiled-net-assemblies

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