problem with togglebutton callback
Show older comments
This is the code of the two togglebuttons which i created in guide. when i press the first togglebutton i activate the second one too, however the code in the first togglebutton isn't executed.
After i call the
togglebutton2_Callback(handles.togglebutton2, eventdata, handles)
nothing appears at the edit1 as i expect.
The code in button2 runs normally and i see the results at edit2.
function togglebutton1_Callback(hObject, eventdata, handles)
ispushed=get(hObject,'Value');
if ispushed
set(hObject, 'String', 'STOP');
else
set(hObject, 'String', 'START');
end
set(handles.togglebutton2, 'Value', 1);
togglebutton2_Callback(handles.togglebutton2, eventdata, handles)
x=0;
set(handles.edit1, 'String', x);
function togglebutton2_Callback(hObject, eventdata, handles)
tic;
while get(hObject,'Value')
b=toc;
set(handles.edit2, 'String', floor(b));
drawnow
end
What am i doing wrong?
And one more question, how do i stop the execution of the code in button2 by pressing the button1?
1 Comment
Adam
on 7 Mar 2016
As it stands the loop in togglebutton2_callback is infinite which is why your code in togglebutton1_callback never completes and edit1 is never updated.
Pressing togglebutton1 again will simply queue the instruction until togglebutton2 has finished executing which it never will.
Accepted Answer
More Answers (1)
Adam
on 7 Mar 2016
function togglebutton1_Callback(hObject, eventdata, handles)
ispushed=get(hObject,'Value');
if ispushed
set(hObject, 'String', 'STOP');
else
set(hObject, 'String', 'START');
end
set(handles.togglebutton2, 'Value', 1);
if ispushed
togglebutton2_Callback(handles.togglebutton2, eventdata, handles)
x=0;
set(handles.edit1, 'String', x);
end
function togglebutton2_Callback(hObject, eventdata, handles)
tic;
while get(hObject,'Value') && get( handles.togglebutton1, 'Value' )
b=toc;
set(handles.edit2, 'String', floor(b));
drawnow
end
should work.
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!