MATLAB: Using a Matlab GUI Button to Halt a running callback

gui button run halt callback

I have a Matlab GUI with a button initially called 'Run'. When I hit that button a loop is started that does some processing, and I change the name of the button to 'Halt'. When the 'Halt' button is pressed, I want the GUI to recognize the button press, and halt the callback that is running. So my question is, is there any property of a Button that I can use that will be changed when my user hits my button the 2nd time? (If I can detect the button has been pressed, then I can halt the running loop).

Best Answer

This can be done but for ease i would change to a toggle button. Save the below code as a whole under opti.m and then run it. without spending too much time i was trying to do it with a push button and update flags but haven't had to time to think through the handle update flow. however since the toggle button is handled at the handle level (MATLAB employees please correct me on that) and not running another function to update a value it will update while in the loop.
function opti();
fig = figure;
ax = axes;
axis([0 100 -5 5]);
b = uicontrol('Style', 'togglebutton', 'String', 'RUN',...
'Position', [20 20 50 20],...
'Callback', {@buttonpress,ax});
end
function buttonpress(hObject,event,ax)
switch get(hObject,'Value')
case 0
set(hObject,'String','HALT')
case 1
set(hObject,'String','RUN')
cla(ax);
end
axes(ax)
hold on;
t=0;
while get(hObject,'Value') || t>30;
t=t+1;
y=sin(2*pi*t/10);
plot(t,y,'.');
pause(.1);
end
end