3

I'm making a GUI with Matlab's guide. I'm placing points with impoint, and I use addNewPositionCallback to be able to update my 'point list'. One of the arguments given to my update function that I give as a callback, is the 'handles' object. But Matlab passes this by value, so when the callback is called, I do have the handles object there but it's an outdated version. I would like to have something like a pointer to the handles object.

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

Edit: So I have a callback function that look 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 pointlist in the handles, handles.pointlist. It should contain 5 points, but when I have an updatePosition call for the first point, the list contains just one point: the handles does not seem to be updated, it just has a copy from earlier on.

Castilho
  • 3,147
  • 16
  • 15
user1254962
  • 153
  • 5
  • 15

2 Answers2

3

Like javascript, matlab script can create closures as function handles. That means it can 'capture' variables. You can create updatePosition in a context where you do have access to the handles 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);
Ian Shi
  • 31
  • 3
0

If you design your GUI using GUIDE, handles.output store the handles to the main interface. So if you add this line in your callback :

handles=guidata(handles.output);

it should update your handles to the current version. You can get some details on all these in here : http://www.matlabtips.com/guide-me-in-the-guide/