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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With