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

43 views (last 30 days)
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('inside loop');
function pushbutton4_Callback(hObject, eventdata, handles)
if get(handles.pushbutton4,'userdata')==1
Hassan Bosha
Hassan Bosha on 17 Feb 2019
What is the user data ?
should it be written like this ?
function Resume_Callback(hObject, eventdata, handles)
handles.Resume.UserData = ~handles.Resume.UserData;

Sign in to comment.

Answers (3)

Jan on 7 Aug 2017
Edited: Jan on 9 Aug 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
a = get(handles.pushbutton4, 'userdata');

Image Analyst
Image Analyst on 7 Aug 2017
See attached demo.

Akhilesh Thakur
Akhilesh Thakur on 8 Aug 2017
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.
Image Analyst
Image Analyst on 9 Aug 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.

Sign in to comment.


Find more on Graphics Object Programming 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!

Translated by