Help using getmcruserdata and setmcruserdata with JAVA
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Artik Crazy
2011-10-13
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 个)
Artik Crazy
2011-10-13
Hi, try this:
Object[] result = null;
result = myClass.getmcruserdata(key_string);
FirstOut=((MWNumericArray) result[0]).getDouble();
SecondOut=((MWNumericArray) result[1]).getDouble();
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!