question about togglebutton in gui
1 次查看(过去 30 天)
显示 更早的评论
Hello!
i have created a gui and i have a togglebutton in it.
the code in the while loop in the togglebutton works fine.
while get(hObject,'Value')
/* code */
end
1
My problem is that the code outside the while loop is not executed. for the above example,when i press the togglebutton the code works fine and when i release it the '1' is not appeared.
Any idea?
thank you very much
0 个评论
采纳的回答
Geoff Hayes
2014-10-31
Alex - without knowing the contents of your code (in the while loop) I can only hypothesize that the problem may be that the code is not giving any other callback a chance to interrupt this one. While the interruptible property of this toggle (see this in the Property Inspector) may be on, it will only be interrupted if the function calls drawnow, figure, getframe, pause, or waitfor at each iteration of the while loop.
Try the following
function togglebutton1_Callback(hObject, eventdata, handles)
while get(hObject,'Value')
% do stuff
end
fprintf('exiting while loop!\n');
If I run my GUI, and press the toggle button, then the code is stuck in the while loop even though I depress the toggle and so I have to ctrl+c to cancel the operation.
In order to allow the function to be interrupted, I could change the above to include a pause or drawnow at each iteration (the latter may be inappropriate if you are not drawing anything)
function togglebutton1_Callback(hObject, eventdata, handles)
while get(hObject,'Value')
% do stuff
pause(0.001); % or drawnow
end
fprintf('exiting while loop!\n');
Now, when you run your GUI and press the toggle, once you depress it, the while loop will exit and you will see
exiting while loop!
exiting while loop!
written twice to the Command Window. It is written once for the original callback that becomes interrupted, and a second time because you have invoked the callback by depressing the toggle button. This could have negative side effects if you have some code that follows the while loop which you only want to be invoked if the code has just exited the while loop. You could guard against this sort of thing by only running the code in the callback if the toggle is pressed
function togglebutton1_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
while get(hObject,'Value')
% do stuff
pause(0.001);
end
fprintf('exiting while loop!\n');
end
and you will only see the message written once to the Command Window.
An alternative approach (which I typically use) is to create a timer - when the user presses the toggle button, start the periodic timer that will repeatedly call the code that you would have had in your while loop. And when the user depresses the toggle button, stop the periodic timer. This may not be applicable/possible for your code though (it all depends on what is happening in the loop).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!