Handling user-defined matlab class returned from Java collection

3 Ansichten (letzte 30 Tage)
John
John am 30 Aug. 2014
Beantwortet: Yair Altman am 4 Sep. 2014
Suppose I have a user defined MATLAB class like this:
classdef A
properties
property1;
property2;
end
methods
function obj = A()
obj.property1 = 1;
obj.property2 = 2;
end
end
end
And then instantiate class A like this:
a = A();
Then I instantiate a Java collection in MATLAB like this:
list = java.util.ArrayList();
Then I encapsulate that instance of class A into a cell and add it into that ArrayList like this:
list.add({a});
It has to be encapsulated into a cell for it to work for some obvious reasons.
Then I retrieve that instance of class A back from the ArrayList like this:
b = list.get(0);
What I get in b is
java.lang.Object[]
I have read the section on handling values returned Java values and I infer that there is no way to recover or cast that returned java.lang.Object to a type of class A.
Or is there? I am trying to shove in user-defined MATLAB types into Java collections because MATLAB does not offer the richness of collection manipulations that Java does. The above would have worked if I used one of the built in MATLAB types, but my interest is specific to user-defined MATLAB types. I suppose another way would be to create a Java class that implements the functionality of my user-defined MATLAB Class and store that into the a Java collection instantiated within MATLAB.
Any suggestions?
  1 Kommentar
John
John am 30 Aug. 2014
Upon reading the MATLAB Java interface documentation it has become apparent to me that I cannot shove in user-defined MATLAB classes into Java collections. I would appreciate commentary from a MATLAB techie

Melden Sie sich an, um zu kommentieren.

Antworten (2)

per isakson
per isakson am 31 Aug. 2014
Bearbeitet: per isakson am 31 Aug. 2014
Are you aware of the Matlab-Java book? That or the blog Undocumented Matlab might have the answer to your question.

Yair Altman
Yair Altman am 4 Sep. 2014
The solution is to simply convert the Matlab object (any non-primitive Matlab data) into something that Java knows. Here are 3 ways to do this:
  1. Use the toJava() function from the opensource Lightspeed toolbox (reverse: fromJava())
  2. Use your own dedicated converter to convert the Matlab data into a Java object.
  3. Use Matlab's builtin serialization functions getByteStreamFromArray and getArrayFromByteStream, as described here. This converts Matlab data into a byte array that Java accepts natively ( usage example with Java classes ).
Using these methods we can convert any Matlab data to become Java-compatible, enabling usage of the Java Collection classes (among others). This provides not just much richer functionality compared to Matlab's data structures, but also offers much better performance. I'm showing how to do this in my book Accelerating MATLAB Performance (CRC Press, 12/2014).

Kategorien

Mehr zu Call Java from MATLAB 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!

Translated by