Does OOP work with Builder JA
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
If classdef is used in MATLAB then are built using Builder JA, how are properties and methods accessed in Java? Thanks...
0 Kommentare
Antworten (2)
Joan Puig
am 25 Jun. 2011
You can't really use MATLAB objects as if they were normal Java objects.
Your MATLAB objects (and primitive types) will become some form of the MWArray class in the Java side: http://www.mathworks.com/help/toolbox/javabuilder/MWArrayAPI/index.html
What we do instead is have MATLAB functions that will manipulate this objects from the MATLAB side in batches, and those are the functions we actually call from Java.
The reason why you want to group as many operations as possible for every call from Java to the MCR is that there is certain overhead.
0 Kommentare
Explorer01
am 13 Jan. 2014
So a possible approach would be to define a MATLAB class like
classdef TestClass
% Local variables
properties (GetAccess = private, SetAccess = private)
var1;
var2;
end
% Public adjustable properties
properties (GetAccess = public, SetAccess = public)
var3 = 100;
end
methods
function obj = TestClass()
obj.var1=0;
obj.var2=0;
if isdeployed
disp('Deployed on MCR');
else
disp('Running in Matlab');
end
end
function result = doSomething01(obj, param1)
result = param1 + obj.var3;
end
function result = doSomething02(obj, param1)
result = param1 - obj.var3;
end
end
end
For each of the methods in the MATLAB class, you should create a separate MATLAB function to use the orignal class.
function [ result ] = construct( )
result = TestClass();
end
function [ result ] = doSomething01( object, param01 )
result = object.doSomething01(param01);
end
function [ result ] = doSomething02( object, param01 )
result = object.doSomething02(param01);
end
The idea in the functions above is to use construct to create an instance of the MATLAB class and then pass this instance as a parameter to perform the object manipulations.
Those separate functions will then be exported to a single JAR file containing one class (ExportedMatlab) containing the contruct.m, doSomething01.m and doSomething02.m files and the TestClass.m as a shared resource/helper file.
In Java we then can create an instance of the exported ExportedMatlab class by using the statement:
builder = new ExportedMatlab();
On this instance, we can call the construct function by using the following code:
List<Object> mtlInputs = new ArrayList<Object>();
List<Object> mtlOutputs = Arrays.asList(new Object[1]);
builder.construct(mtlOutputs, mtlInputs);
obj = (MWJavaObjectRef)mtlOutputs.get(0);
My guess was this would return an instance of the MATLAB object, stored in the obj variable, which I could use to pass as a parameter to the next function call in Java.
List<Object> mtlInputs = new ArrayList<Object>();
List<Object> mtlOutputs = Arrays.asList(new Object[1]);
mtlInputs.add(obj);
mtlInputs.add(10);
builder.doSomething01(mtlOutputs, mtlInputs);
When calling the contruct function, Java execution fill fail resulting in a "java.lang.RuntimeException: Could not create MWArray from unsupported MATLAB type MCOS". Not suprisingly off course since an object reference is not an array. My question is how can we actually solve this problem and provide a solution allowing us to pass a MATLAB object reference created in MATLAB to the Java world and use this instance for manipulation. Why? Since we don't want to recreate the MATLAB object in every exported function to Java.
In other words, we don't want to write MATLAB functions like
function [ result ] = doSomething01( param01 )
object = TestClass();
result = object.doSomething01(param01);
end
since they would create a huge overhead especially when object construction requires a lot of initialisation.
Thanks for the advise. Greetings.
1 Kommentar
Friedrich
am 13 Jan. 2014
Bearbeitet: Friedrich
am 13 Jan. 2014
"MATLAB Objects
In addition, the MATLAB Builder JA product does not support MATLAB object data types (for example, Time Series objects). In other words, MATLAB objects can not "pass" the boundary of MATLAB/Java, but you are free to use objects in your MATLAB code. "
So consider using persistent variables or globals to keep the instance of the MATLAB object alive on the MATLAB side.
(Back in the older days the following worked, but not sure if it still works: One could pass down the MATLAB class instance when it was contained in a struct, e.g. out.class = TestClass(). You got the class instance on the JAVA side but couldn't do anything directly with it. However one could pass it back in to the MCR and work on it)
Siehe auch
Kategorien
Mehr zu Java Package Integration finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!