how can i have a button in one callback function pause the execution of another callback function

3 次查看(过去 30 天)
function pushbutton1_Callback(hObject, eventdata, handles)
%when this button is pressed i want the code that is executing in
%pushbutton2 to pause
end
function pushbutton2_Callback(hObject, eventdata, handles)
%some code
end
%i am using matlab version R2019b
%Thank you
  1 个评论
Adam Danz
Adam Danz 2021-5-27
编辑:Adam Danz 2021-5-27
@Joseph Kutteh this question doesn't differ much from the question you asked earlier this week and never replied to:
And it's no surprise that both questions describe the same solution with some minor differences.
How many people do you want working on your problem? Please take some time to learn from the solutions and if you've got follow-up questions, leave comments.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-5-27
编辑:Jan 2021-6-1
% Initialize in the CreateFcn: handles.Pause = false
function pushbutton1_Callback(hObject, eventdata, handles)
handles.Pause = (hObject.Value == 0);
guidata(hObject, handles);
end
function pushbutton2_Callback(hObject, eventdata, handles)
for k = 1:1000
disp(clock); % <-- This is the pseudo code
pause(0.5); % <-- Insert your real computations instead
% If button1 was pressed, stay in this loop:
while handles.Pause % [EDITED] WHILE instead of IF ?!
handles = guidata(hObject);
pause(0.2);
end
end
end
  3 个评论
Jan
Jan 2021-6-1
I do not understand, what you are asking for. I've marked the dummy lines now, which I have inserted instead of the real computations.
Adam Danz
Adam Danz 2021-6-1
编辑:Adam Danz 2021-6-1
The while loop in Jan's answer 'pauses' execution if the pause-flag is set to true. This flag is set by pressing pushbutton1 which must be a togglebutton that has a binary state (on/off) (see the pushbutton1_Callback function in Jan's answer).
Note that execution isn't really paused. It's just stuck in the while loop until it gets the signal to escape.
Before you sink too much time into a GUI that appears to be created by GUIDE, note that,
>The GUIDE environment will be removed in a future release. After GUIDE is removed, existing GUIDE apps will continue to run in MATLAB® but they will not be editable in GUIDE - noted in r2021 documentation.
Other ways to implement pauses in Apps/GUIs.
  • waitfor - example using AppDesigner but can be implemented outside of AppDesigner, too.
  • uiprogressdlg with the Indeterminate option (requires uifigure which excludes GUIDE GUIs)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by