Help using getmcruserdata and setmcruserdata with JAVA
조회 수: 1 (최근 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!