question about togglebutton in gui

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

 Accepted Answer

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).

4 Comments

Hello again Geoff!
As you understood, in the while loop there is the code for the drawing that we discussed earlier.
So the code is
while get(hObject,'Value')
if pentouschestablet
x = [x ; newX];
y = [y ; newY];
set(hLines(end),'XData',x,'YData',y);
drawnow;
end
if pennottouschestablet
x=[];
y=[];
hLines = [hLines line(NaN,NaN, 'Color','k','LineWidth', 1)];
end
end
it works fine. when i press the button i can draw and when i release it i cannnot.
However,outside the while loop i want to have a command so i can close the connection with the tablet.so when i release the button,automatically the connection with the tablet is closed.
But,for some reason,the code never goes to that command.
Thanks for your answer, i will try the if statement!
Alex - the problem may be with the second if statement. If the toggle is still pressed and the pen is no longer touching the tablet, then we will always enter the second if statement which does not have a pause or drawnow command, and so cannot be interrupted. Try modifying it to
if pennottouschestablet
x=[];
y=[];
hLines = [hLines line(NaN,NaN, 'Color','k','LineWidth', 1)];
pause(0.001);
end
and see what happens.
yes that's the answer! i raise the pen when i stop drawing,so the code enters the second if statemenet!thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 31 Oct 2014

Commented:

on 3 Nov 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!