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

6 ビュー (過去 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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by