How can I update the values of a "handles" struct, if I want to use a function, in MATLAB GUI?
Show older comments
For example I set the following listener for hUE(n) to ButtonDownFcn when the user click on the object (which the handle is hUE(n)), the removeUE is called and sends the "handles" struct to it:
set(hUE(n),'ButtonDownFcn',{@removeUE,handles});
I have used the global variables to store the values of hUE and update it in the function as you see below:
function removeUE(src,evnt,handles)
global UECounter;
global hUE;
global flag;
if strcmp(flag,'removeUE')
hUE = hUE(hUE ~= src);
delete(src);
UECounter = UECounter - 1;
end
My question is how can I store the values of hUE(n) in the "handles" struct and get rid of the global variables?
Answers (1)
Walter Roberson
on 30 Apr 2013
if strcmp(handles.flag,'removeUE')
handles.hUE = handles.hUE(handles.hUE ~= src);
handles.UECounter = handles.UECounter - 1;
guidata(src, handles);
delete(src);
end
4 Comments
Armin Morattab
on 1 May 2013
Walter Roberson
on 1 May 2013
Yes it does. However, if the event occurs during the middle of execution of another routine, that other routine's notion of "handles" will not be updated until that routine asks for an update by using
handles = guidata(AppropriateFigureNumber);
The "handles" that is passed into a routine should be treated like a photocopy of the "master" copy of the information. You can mark up the photocopy all you want, and you can make the master a copy of your marked-up version, but anyone else who photocopies the master version will not get your changes until you post the changes to the master and the other person goes back and asks for a new copy.
Armin Morattab
on 1 May 2013
Walter Roberson
on 1 May 2013
When you use
set(handles.axes1,'ButtonDownFcn',{@addUE,handles})
you are setting the argument to be passed in to addUE to be a copy of the handles structure as it is at the time the ButtonDownFcn is set(). That is not what you want. So instead of passing in handles, pass in the figure number of the GUI, and inside each of those routines use
handles = guidata(ThePassedFigureNumber);
and then if the routine updates handles,
guidata(ThePassedFigureNumber, handles);
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!