problem with togglebutton callback

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

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.

Sign in to comment.

 Accepted Answer

alex - look closely at the code in your togglebutton1 callback
set(handles.togglebutton2, 'Value', 1);
togglebutton2_Callback(handles.togglebutton2, eventdata, handles)
x=0;
set(handles.edit1, 'String', x);
You set the string for the button and then call the callback for togglebutton2 which includes a while loop. And so long as the condition of the while loop is true, we will never exit the togglebutton2_Callback function and control will never return to togglebutton1_Callback...which means that x never gets initialized and the edit1 control never gets updated. So you probably want to do this before calling the other function.
I don't understand the purpose of the second toggle button. Why is it necessary if the first toggle button starts the counter? Would the user ever push the second toggle button callback? If absolutely necessary, then you could modify your code to be as follows
function togglebutton1_Callback(hObject, eventdata, handles)
ispushed=get(hObject,'Value');
if ispushed
set(hObject, 'String', 'STOP');
x=0;
set(handles.edit1, 'String', x);
set(handles.togglebutton2, 'Value', 1);
togglebutton2_Callback(handles.togglebutton2, eventdata, handles)
else
set(hObject, 'String', 'START');
set(handles.togglebutton2,'Value',0);
end
The above is similar to what you have shown. Note that in the body of the else we set the Value property to zero for the second toggle button so that we exit from the while loop in its callback.
You may want to reconsider some of this code. Ask yourself if the second toggle button is necessary and how you may just use the first one to control the updates to the edit boxes.

5 Comments

Dear Geoff,
what i want to do is this: i press the button,and i want to start a time counter which i want to display in the edit2, but also i want to wait until the user press a button (using the waitforbuttonpress).
So in the edit1 will appear the number of mouseclicks,that the user did, an in the edit2 there will be the chronometer. I can't do these two things in the same togglebutton, because when the waitforbuttonpress is waiting for a mouseclick, then the chronometer would stop and will continue when the user click the mouse.
for this reason i want two different buttons beacause i think that in the first one there should be the waitforbuttonpress, and in the second button the time counter.
Geoff Hayes
Geoff Hayes on 7 Mar 2016
Edited: Geoff Hayes on 7 Mar 2016
Alex - by ..number of mouse clicks.. do you mean the number of times that the user has pressed the START button (toggling the first toggle button on)? Or do you mean that you are somehow counting the number of mouse clicks made by the user?
Where are you using waitforbuttonpress? Is that something that you want to implement or have implemented elsewhere in the code?
Your statement but also i want to wait until the user press a button (using the waitforbuttonpress). Which button are you referring to?
I'm sorry if i'm not very clear. From the moment i press the togglebutton, i want to count the number of mouseclicks the user does, and i also want to show a time counter.When i press again the togglebutton i want to stop the time counter and also stop counting the number of mouseclicks.
The structure should be like this:
togglebutton1
while pressed
start chronometer
show chronometer
waitforbuttonpress
if waitforbuttonpress==1 %if user made a mouseclick
counter=counter+1;
show counter
end
end
stop chronometer
Alex - in your above pseudocode, you don't mention the second toggle button. So how is that used?
And rather than using a while loop, I would do something similar to the following pseudocode in the togglebutton1 callback
if togglebutton1 is pressed
start periodic timer to update chronometer
set mouse click count to zero
else
stop periodic timer
end
And then, I would create a ButtonDownFcn callback for my GUI (figure) that would be
function figure1_ButtonDownFcn(hObject, eventdata, handles)
if get(handles.togglebutton1,'Value')
numMouseClicks = str2num(char(get(handles.edit1,'String')));
if isempty(numMouseClicks)
numMouseClicks = 0;
end
numMouseClicks = numMouseClicks + 1;
set(handles.edit1,'String',num2str(numMouseClicks));
end
The above callback would be responsible for updating the edit1 text control so long as the togglebutton1 has been pressed.
As an aside, you may wish to use static text controls instead of the edit text control (unless you expect the user to update them?).
For the timer, see Create object to schedule execution of MATLAB commands for details on how to start a periodic timer with a callback that will update the other text control.
Thank you Mr Geoff, the periodic timer is what i need, i didn't know its excistance. Hace a nice day!

Sign in to comment.

More Answers (1)

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.

1 Comment

i guess i wasn't clear enough about what i'm trying to do.
Adam, whith your code when the code finishes at the togglebutton2 THEN the code in togglebutton1 continues.
What i want is that both codes in both buttons run simultaneously. I don't want to wait when i call the togglebutton2_Callback until the code in button2 finish, but i want to continue immidiately in the next line.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 7 Mar 2016

Commented:

on 7 Mar 2016

Community Treasure Hunt

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

Start Hunting!