Matlab GUI, need an object with handles

I am creating a GUI with Matlab manual. I am putting points with impoint and I am using addNewPositionCallback to be able to update my "list of points". One of the arguments to my update function that I provide as a callback is the "handles" object. But Matlab passes this value, so when the callback is called, I have a handle object, but it's an outdated version. I would like to have something like a pointer to a handles object.

Or more generally: I would like to access the "handle" object somewhere in the function where I don't have it as a parameter.

Edit: So I have a callback function that looks like this:

function updatePosition(pos, hObject, handles)

Which I add as a callback like this:

addNewPositionCallback(testh,@(pos) updatePosition(pos, hObject, handles));

And I have a pointer in descriptors handles.pointlist

. It should contain 5 points, but when I have a call to updatePosition for the first point, the list only contains one point: the handles don't seem to be updating, it just has a copy from an earlier time.

+3


source to share


2 answers


Just like javascript, matlab script can create closures as functions. This means that it can "capture" variables. You can create updatePosition in a context where you have access to the handle object. You should do it like this:



H = handles.figure1; % get the figure handle
updatePosition = @(p) get(guihandles(H)... % the guihandles(H) contains the handles structure of the figure. Do whatever you need with it.

addNewPositionCallback(testh,updatePosition);

      

+3


source


If you build your GUI with GUIDE, handles.output will keep the main interface handles. So if you add this line to your callback:

handles=guidata(handles.output);

      



it should update your descriptors to the current version. You can get detailed information on all of these here: http://www.matlabtips.com/guide-me-in-the-guide/

0


source







All Articles