Help using getmcruserdata and setmcruserdata with JAVA

2 visualizaciones (últimos 30 días)
Hello
I am trying to used the MCR user memory to access and modify some data both on the Matlab and JAVA sides. I have added both getmcruserdata.m and setmcruserdata.m functions to my class, so that I can access them in JAVA as well.
I am having problems with both using the right types, which don't seem to match the method signatures, as well as converting those to Java types I can use. The signature for getmcruserdata says it takes an object (or object[] if I import it back into matlab and look at methodsview) and returns an object[]. So I code something like this:
Object[] result = null;
result = myClass.getmcruserdata(key_string);
System.out.println(result[0]);
The problem is that it complains that it exceeds the index bounds with index 0. I redefine the result as a single Object type, and the error disappears. But why the mismatch between the signature and the actual usage?
So now it runs, but I can't figure out how to read this data. I've tried converting to String, I've tried casting to MWArray and MWNumericArray, but it always complains that it can't cast an Object into any of those types.
So I was hoping someone would be so kind to guide me on how to call the getmcruserdata method correctly on Java and how to use the data it returns.
For my first simple example, I am passing a single integer. However, in theory I would like to pass more complex data types such as arrays and structures.
I'd be thankful for any tips!
Kind regards Maria

Respuesta aceptada

Artik Crazy
Artik Crazy el 13 de Oct. de 2011
Hi, Try to create two functions in Matlab that contain getmcruserdata and setmcruserdata:
function SetMCRUserData (Value, Key)
setmcruserdata(Key, Value)
end %function
and:
function Value=GetMCRUserData (Key)
Value=getmcruserdata(Key);
end %function
Then, when building the .jar file add these two functions to your class as two methods. (Not in the "Shared Resources" section but in the upper one.)
In Java use them in this way:
String Key = "MyValue";
MyClass.SetMCRUserData(Value, Key);
Object[] Value =null;
Value=MyClass.GetMCRUserData(1, Key);
System.out.println(((MWNumericArray) Value[0]).getDouble());
This solution worked for me, hope it will help you too.
  1 comentario
Maria
Maria el 14 de Oct. de 2011
Excellent, this just worked! It didn't like me adding the built-in functions directly to my class, but wrapping them this way they work like any other method.
Very grateful!

Iniciar sesión para comentar.

Más respuestas (1)

Artik Crazy
Artik Crazy el 13 de Oct. de 2011
Hi, try this:
Object[] result = null;
result = myClass.getmcruserdata(key_string);
FirstOut=((MWNumericArray) result[0]).getDouble();
SecondOut=((MWNumericArray) result[1]).getDouble();
  1 comentario
Maria
Maria el 13 de Oct. de 2011
Thanks for your input. The problem is that, even though the signature states that the output is an Object[], I get the following error:
java.lang.ArrayIndexOutOfBoundsException: 0
If however I do:
Object result = null;
result = myClass.getmcruserdata(key_string);
FirstOut=((MWNumericArray) result).getDouble();
Then I get:
java.lang.Object; cannot be cast to com.mathworks.toolbox.javabuilder.MWNumericArray
Strange!
Could it be that I am not adding the right m files to my class? When I open the getmcruserdata.m function in the editor, it shows the following path:
{matlabroot}\toolbox\matlab\general\getmcruserdata.m
I have added this file to my class before building the project in the deploytool. However, this file only contains a description of the function. Do I need to add a different file than this? Can this be the problem?
Kind regards
Maria

Iniciar sesión para comentar.

Categorías

Más información sobre Java Package Integration en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by