Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Passing java references

Subject: Passing java references

From: Devon Veldhuis

Date: 14 Jul, 2009 14:56:03

Message: 1 of 6

Hi everyone,

My issue is with passing java referenced to and from matlab callback functions. I have an application in which multiple frames are needed, but the frames are all created during callbacks from the main GUI.

I pass a handles structure to all the callbacks, and I want to include a reference to an image (just a 2D array of intensity values for a grayscale image), so that other frames can access the data and change it when needed.

I created a field in the structure handles.image and initialize it to an empty java array with:


handles.image = javaArray('java.lang.Integer', 1, 1);


Then in the function that needs to initialize the value is use:


imageArray = javaArray('java.lang.Integer', height, width);

loop through image array
     imageArray(i,j) = java.lang.Integer(image(i,j));
end loop

handles.image = imageArray;


While stepping through the program, the handles value is assigned properly after that last statement, but as soon as I call another callback, the value of handles.image is just the empty array is was in the beginning. Am I not creating the pointer properly?

Thanks!

Subject: Passing java references

From: Yair Altman

Date: 14 Jul, 2009 21:19:01

Message: 2 of 6

"Devon Veldhuis" <daveldhu@lakeheadu.ca> wrote in message <h3i6a3$74u$1@fred.mathworks.com>...
> Hi everyone,
>
> My issue is with passing java referenced to and from matlab callback functions. I have an application in which multiple frames are needed, but the frames are all created during callbacks from the main GUI.
>
> I pass a handles structure to all the callbacks, and I want to include a reference to an image (just a 2D array of intensity values for a grayscale image), so that other frames can access the data and change it when needed.
>
> I created a field in the structure handles.image and initialize it to an empty java array with:
>
>
> handles.image = javaArray('java.lang.Integer', 1, 1);
>
>
> Then in the function that needs to initialize the value is use:
>
>
> imageArray = javaArray('java.lang.Integer', height, width);
>
> loop through image array
> imageArray(i,j) = java.lang.Integer(image(i,j));
> end loop
>
> handles.image = imageArray;
>
>
> While stepping through the program, the handles value is assigned properly after that last statement, but as soon as I call another callback, the value of handles.image is just the empty array is was in the beginning. Am I not creating the pointer properly?
>
> Thanks!

The handles struct is itself not a pointer/reference, and so it is not automatically persisted. Therefore, when you exit the callback function scope, all the changes you made in handles is lost. To overcome this, you need to persist the data: either use the persistent/global keywords; or set a GUI object's userdata/appdata; or use guihandles() and relatives - all of these will work and the choice depends on your programming tastes (I would personally use userdata/appdata to only store/retrieve the data I need).

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Passing java references

From: Devon Veldhuis

Date: 15 Jul, 2009 13:12:01

Message: 3 of 6

>The handles struct is itself not a pointer/reference, and so it is not automatically persisted. Therefore, when you exit the callback function scope, all the changes you made in handles is lost. To overcome this, you need to persist the data: either use the persistent/global keywords; or set a GUI object's userdata/appdata; or use guihandles() and relatives - all of these will work and the choice depends on your programming tastes (I would personally use userdata/appdata to only store/retrieve the data I need).
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Hi Yair,

Thanks for the reply! I figured out how to solve my problem using guidata(). I was just curious whether the userdata/appdata can be used with a GUI entirely consisting of java objects. It would be really convenient if I could use those properties with my main JFrame so that I always know where to find it, because right now I'm forced to use a matlab figure which may need to be closed at any point during execution to save to with my guidata().

Subject: Passing java references

From: Yair Altman

Date: 28 Jul, 2009 09:42:03

Message: 4 of 6

> Thanks for the reply! I figured out how to solve my problem using guidata(). I was just curious whether the userdata/appdata can be used with a GUI entirely consisting of java objects. It would be really convenient if I could use those properties with my main JFrame so that I always know where to find it, because right now I'm forced to use a matlab figure which may need to be closed at any point during execution to save to with my guidata().


You can use userdata/appdata if you enclose the Java object reference within a Matlab handle, as follows:

hjavaObject = handle(javaObject,'CallbackProperties');
set(hjavaObject,'userdata',...);

See related information here: http://undocumentedmatlab.com/blog/uicontrol-callbacks/

Yair Altman
http://UndocumentedMatlab.com

Subject: Passing java references

From: Arwel

Date: 2 Mar, 2012 14:07:19

Message: 5 of 6

Actually, setting the UserData property of the handle doesn't seem to work.....

>> a = javax.swing.JButton('hello')
 
a = javax.swing.JButton[,0,0,0x0,invalid..... (etc)

>> hObject = handle(a,'CallbackProperties')
 
hObject = javahandle_withcallbacks.javax.swing.JButton

>> set(hObject,'UserData','test')

Error using javahandle_withcallbacks.javax.swing.JButton/set
The name 'UserData' is not an accessible property for an instance of class 'javax.swing.JButton'.
 







"Yair Altman" wrote in message <h4mh5b$bg3$1@fred.mathworks.com>...
> > Thanks for the reply! I figured out how to solve my problem using guidata(). I was just curious whether the userdata/appdata can be used with a GUI entirely consisting of java objects. It would be really convenient if I could use those properties with my main JFrame so that I always know where to find it, because right now I'm forced to use a matlab figure which may need to be closed at any point during execution to save to with my guidata().
>
>
> You can use userdata/appdata if you enclose the Java object reference within a Matlab handle, as follows:
>
> hjavaObject = handle(javaObject,'CallbackProperties');
> set(hjavaObject,'userdata',...);
>
> See related information here: http://undocumentedmatlab.com/blog/uicontrol-callbacks/
>
> Yair Altman
> http://UndocumentedMatlab.com

Subject: Passing java references

From: Yair Altman

Date: 2 Mar, 2012 14:20:18

Message: 6 of 6

"Arwel " <a.v.hughes@rl.ac.uk> wrote in message <jiqk6n$1t5$1@newscl01ah.mathworks.com>...
> Actually, setting the UserData property of the handle doesn't seem to work.....
>
> >> a = javax.swing.JButton('hello')
>
> a = javax.swing.JButton[,0,0,0x0,invalid..... (etc)
>
> >> hObject = handle(a,'CallbackProperties')
>
> hObject = javahandle_withcallbacks.javax.swing.JButton
>
> >> set(hObject,'UserData','test')
>
> Error using javahandle_withcallbacks.javax.swing.JButton/set
> The name 'UserData' is not an accessible property for an instance of class 'javax.swing.JButton'.
>
>
> "Yair Altman" wrote in message <h4mh5b$bg3$1@fred.mathworks.com>...
> > > Thanks for the reply! I figured out how to solve my problem using guidata(). I was just curious whether the userdata/appdata can be used with a GUI entirely consisting of java objects. It would be really convenient if I could use those properties with my main JFrame so that I always know where to find it, because right now I'm forced to use a matlab figure which may need to be closed at any point during execution to save to with my guidata().
> >
> >
> > You can use userdata/appdata if you enclose the Java object reference within a Matlab handle, as follows:
> >
> > hjavaObject = handle(javaObject,'CallbackProperties');
> > set(hjavaObject,'userdata',...);
> >
> > See related information here: http://undocumentedmatlab.com/blog/uicontrol-callbacks/
> >
> > Yair Altman
> > http://UndocumentedMatlab.com


This behavior has indeed changed in some recent Matlab release (I forget which). You can use the ApplicationData property instead (via getappdata/setappdata or the ApplicationData property directly), although that too sometimes fail in recent Matlab releases.

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us