How can pause/stop/resume/run Matlab gui code?

I have developed a code for run/pause the code which is palying a song and plotting a figure. However, inside my while 1, the result of button4 is not update. Where is my fault?
function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.pushbutton4, 'userdata', 1);
while 1
a= get(handles.pushbutton4, 'userdata')
if a==0
disp('break');
break;
else
disp('inside loop');
end
end
function pushbutton4_Callback(hObject, eventdata, handles)
if get(handles.pushbutton4,'userdata')==1
set(handles.pushbutton4,'userdata',0);
else
set(handles.pushbutton4,'userdata',1);
end

2 comentarios

By the way: You can simplify the callback:
function pushbutton4_Callback(hObject, eventdata, handles)
handles.pushbutton4.UserData = ~handles.pushbutton4.UserData;
What is the user data ?
should it be written like this ?
function Resume_Callback(hObject, eventdata, handles)
handles.Resume.UserData = ~handles.Resume.UserData;

Iniciar sesión para comentar.

Respuestas (3)

Jan
Jan el 7 de Ag. de 2017
Editada: Jan el 9 de Ag. de 2017
You cannot directly stop Matlab code by a GUI. Note that the callback of the GUI would be stopped also.
You can control the execution of a loop through a GUI, if you request e.g. the UserData of a certain button, which is toggled in its callback.
What is the actual problem you want to solve?
[EDITED] Give the figure a chance to update its properties by inserting drawnow:
while 1
drawnow;
a = get(handles.pushbutton4, 'userdata');
...

3 comentarios

amir nemat
amir nemat el 8 de Ag. de 2017
Please see the updated question
Jan
Jan el 9 de Ag. de 2017
See [EDITED]
Thanks for your help, Jan ! This is the second time you save my app.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 7 de Ag. de 2017

0 votos

See attached demo.

2 comentarios

amir nemat
amir nemat el 8 de Ag. de 2017
please see the updated question
Image Analyst
Image Analyst el 8 de Ag. de 2017
You need to call guidata().

Iniciar sesión para comentar.

Akhilesh Thakur
Akhilesh Thakur el 8 de Ag. de 2017

0 votos

function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.pushbutton4, 'userdata', 1);
This set(handles.pushbutton4, 'userdata', 1); is wrong. Because you are writing handles.pushbutton4 in the callback of pushbutton1. You are missing guidata(hObject,handles) in your pushbutton4 callback. This will update your handles structure and it will work.

6 comentarios

This won't work unless you add guidata(hObject,handles). This should make your program work.
Hope this helps
Image Analyst
Image Analyst el 9 de Ag. de 2017
I had already suggest this, and we're waiting to hear back.
Eric Keller
Eric Keller el 9 de Ag. de 2017
I use getappdata/setappdata for semaphores. guidata just seems too complex and almost surely uses the same mechanism. You can have as many named appdata structures as you want.
Image Analyst
Image Analyst el 9 de Ag. de 2017
That would work too. Both getappdata/setappdata and guidata are just one line of code.
Jan
Jan el 9 de Ag. de 2017
Editada: Jan el 9 de Ag. de 2017
I do not think that guidata or setappdata help here. The OP did not change the handles struct, but used it to set the UserData of the button. Then only drawnow is missing to allow an update of the properties.
Image Analyst
Image Analyst el 9 de Ag. de 2017
Isn't the UserData a property of the button and thus stored in the handles structure along with all the other properties of the button? And if you change handles, like a field of it or adding a new field, those changes are local and vanish once the function exists unless you call guidata, or pass handles out as a return argument if the function is a custom function rather than a callback function of a control.
And I thought drawnow just repaints the screen which will just show the same screen unless some property of the control which is visible had been changed.

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Ag. de 2017

Comentada:

el 21 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by