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

6 Ansichten (letzte 30 Tage)
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 Kommentar
Adam Danz
Adam Danz am 27 Mai 2021
Bearbeitet: Adam Danz am 27 Mai 2021
@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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 27 Mai 2021
Bearbeitet: Jan am 1 Jun. 2021
% 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 Kommentare
Jan
Jan am 1 Jun. 2021
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 am 1 Jun. 2021
Bearbeitet: Adam Danz am 1 Jun. 2021
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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by