필터 지우기
필터 지우기

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

조회 수: 19 (최근 30일)
amir nemat
amir nemat 2017년 8월 7일
댓글: Luis Cavalcante Fraga 2019년 2월 21일
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
Jan
Jan 2017년 8월 9일
By the way: You can simplify the callback:
function pushbutton4_Callback(hObject, eventdata, handles)
handles.pushbutton4.UserData = ~handles.pushbutton4.UserData;
Hassan Bosha
Hassan Bosha 2019년 2월 17일
What is the user data ?
should it be written like this ?
function Resume_Callback(hObject, eventdata, handles)
handles.Resume.UserData = ~handles.Resume.UserData;

댓글을 달려면 로그인하십시오.

답변 (3개)

Jan
Jan 2017년 8월 7일
편집: Jan 2017년 8월 9일
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
Jan
Jan 2017년 8월 9일
See [EDITED]
Luis Cavalcante Fraga
Luis Cavalcante Fraga 2019년 2월 21일
Thanks for your help, Jan ! This is the second time you save my app.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2017년 8월 7일
See attached demo.
  댓글 수: 2
amir nemat
amir nemat 2017년 8월 8일
please see the updated question
Image Analyst
Image Analyst 2017년 8월 8일
You need to call guidata().

댓글을 달려면 로그인하십시오.


Akhilesh Thakur
Akhilesh Thakur 2017년 8월 8일
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
Jan
Jan 2017년 8월 9일
편집: Jan 2017년 8월 9일
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 2017년 8월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by