MATLAB: How to simply detect a push button click

buttoncallbackpushpushbutton

Hello all,
I've been trying lots of different methods of using a push button callback and I can't seem to find a simple way to detect a click and then do some miscellaneous thing. I'm trying to avoid making the callback a function call since I am trying to update variables in my main loop and run separate functions based on those variables. I could probably do this with global variables but that seems like overkill.
I was hoping for something like this:
press = 0;
button1 = uicontrol('style', 'pushbutton', 'string', 'button1', 'callback', 'press = 1');
if press
x = x + 1;
myfunction(x);
end
This doesn't work though since "press" doesn't exist outside of the callback. Next I tried using guidata to save "press" but that didn't work either.
press = 0;
button1 = uicontrol('style', 'pushbutton', 'string', 'button1', 'callback', 'press = 1; guidata(figure, press)');
press = guidata(figure);
if press
x = x + 1;
myfunction(x);
end
Any thoughts?

Best Answer

When you use a string to specify a callback, the string is eval()'d in the base workspace, so you could use
if evalin('base', 'press')
but that would not be recommended. In a situation like yours I would suggested nested functions with shared variables.