Java to Matlab and back to Java

3 views (last 30 days)
Partha
Partha on 7 Aug 2014
Edited: per isakson on 21 Nov 2014
I am trying to compile and make available Matlab functions via a jar to be used in a Java application. I thought, given the support for Java collections in Matlab - meaning I can, say, create and use a java.util.HashMap in the matlab code - perhaps, I can pass it back to the calling Java method.
This part seems to work just fine. Meaning, I can make the round trip to matlab and back. However, the returned value from the matlab call is a MWJavaObjectRef, which I am not able to cast into a HashMap within the calling routine. I get the following error:
INFO 2014-08-06 15:26:08,408 [main] Server - Result type: com.mathworks.toolbox.javabuilder.MWJavaObjectRef Exception in thread "main" java.lang.ClassCastException: com.mathworks.toolbox.javabuilder.MWJavaObjectRef cannot be cast to java.util.HashMap at main.Server.initializeMatlab(Server.java:55) at main.Server.main(Server.java:34)
The question then is:
1. Can you pass such java collections (created in matlab) back to Java? 2. It seems elegant, if it works. But, is this the prescribed way to do it?
My returned HashMap has values, which are themselves java.util.ArrayList and java.util.HashMap objects.
Any experiences, examples etc. would be greatly appreciated.
Here is my Java code:
// Test the matlab call
calcEngine = new CalculationEngine();
Object [] args = new Object[0];
Object[] result = calcEngine.CalcEngineTester(1,args);
log.info("Result type: " + result[0].getClass().getCanonicalName());
Object tempObj = (Object) result[0];
Map<String,Object> outputMap = (HashMap<String,Object>) tempObj; <<< This is what fails!
// Matlab code
...
...
output1 = java.util.ArrayList();
output2 = java.util.HashMap();
outputMap = java.util.HashMap();
outputMap.put('A', output1);
outputMap.put('B', output2);

Answers (0)

Categories

Find more on Java Package Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!