Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invoke Matlab object's (directory) method from .Net

Tags:

c#

matlab

I defined a class with a bunch of methods stored in a method directory. I can instantiate the class and invoke its methods within matlab. However, if I try to do the same from .NET/COM I get this error messages:

"??? Reference to non-existent field 'test'.\n\n"

Here test is the method.

My class is derived from handle and I have tried both possibilities: method defined in class file and directory method. Neither works!

Any feedback would be very much appreciated. Many thanks.

PS:

C# code:

MLApp.MLApp matlab = new MLApp.MLApp();

matlab.Execute("clear;");
matlab.Execute("Object = Class1();");
string test = matlab.Execute("Object.test()");

Working matlab code:

clear;
Object = Class1();
Object.test()

PPS:

Just double checked that the working Matlab script is NOT working when invoked from C# code:

Matlab class definition:

classdef Test < handle
    methods         
        function [c, obj] = add(obj, a, b) 
            c = a + b;  
        end
    end % methods
end %classdef

Matlab script:

clear;
Test = Test();
result = Test.add(1, 3);

C# code:

MLApp.MLApp matlab = new MLApp.MLApp();

object result;

matlab.Execute("clear;");
matlab.Execute("Test = Test();");
matlab.Execute("result = Test.add(1, 3);");
matlab.GetWorkspaceData("result", "base", out result);
like image 410
cs0815 Avatar asked Aug 31 '12 16:08

cs0815


1 Answers

It turns out that you cannot use the same 'object instance name' as the class name. So:

MLApp.MLApp matlab = new MLApp.MLApp();

object result;

matlab.Execute("clear;");
matlab.Execute("X = Test();");
matlab.Execute("result = X.add(1, 3);");
matlab.GetWorkspaceData("result", "base", out result);

works! Mathworks raised this an error (they may fix this in future releases).

Christian

like image 54
cs0815 Avatar answered Sep 28 '22 21:09

cs0815