How to nest callback into while loop?

1 Ansicht (letzte 30 Tage)
Sergey Lopatnikov
Sergey Lopatnikov am 11 Okt. 2021
I write simple program with a while loop
Within the while loop in each cicle the figure with two buttons: "CONTINUE" and 'FINISH' created.
As expected, if one preses the CONTINUE button, the cicle repeats, in case of presing FINISH, the while loop is finishing.
The problem is that if callback functions for button place within the "while" loop the MATLAB says that callback function(s) is incorrectly nestled.
If callback functions for button place outside the "while" loop, after pressing one of the buttons the MATLAB says that 'event_data' in callback function is not recognized.
function [TGA] = TGA_MULTY_LOAD()
IndJ=0
while IndJ==0
%TGA(:,:,:,IndJ+1)=TGA_LOAD()
TEST=1
Xa=500;
Ya=400;
La=500;
Wa=300;
p=80;
r=150;
s=150;
t=50;
p1=290;
r1=150;
s1=150;
t1=50;
STR_1='CONTINUE';
STR_2='FINISH'
T=[];
handles.cont_fig=figure('Name','Continue','MenuBar','none','Position',[Xa,Ya,La,Wa]);
handles.continue_button=uicontrol(handles.cont_fig,'style','pushbutton','string',STR_1,'FontWeight','bold','FontSize',14,'position',[p,r,s,t])
set(handles.continue_button,'callback',{@continue_callback,handles})
handles.finish_button=uicontrol(handles.cont_fig,'style','pushbutton','string',STR_2,'FontWeight','bold','FontSize',14,'position',[p1,r1,s1,t1])
set(handles.finish_button,'callback',{@finish_callback,handles})
IndJ1=0;
uiwait(gcf)
continue_callback(gcf,event_data,handles)
finish_callback(gcf,event_data,handles)
IndJ=IndJ1
end
function continue_callback(gcf,event_data,handles)
IndJ1=0
close(handles.cont_fig)
end
function finish_callback(gcf,event_data,handles)
IndJ1=1
close(handles.cont_fig)
end
Q='End'
end
  2 Kommentare
Jan
Jan am 12 Okt. 2021
Please format your code properly. Use the standard indentation (press Ctrl-a Ctrl-i in the editor) and use the Code-button on top of the field for editing your question.
Sergey Lopatnikov
Sergey Lopatnikov am 12 Okt. 2021
Thank you. Is it OK now?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 13 Okt. 2021
@Sergey Lopatnikov - the _'event_data' in callback function is not recognized_ error message makes sense since you are trying to use a variable doesn't exist. To avoid this error, just pass an empty array into the callback
continue_callback(gcf,[],handles)
But is this really what you want to do? After the call to uiwait, you call both the continue and finish callbacks
uiwait(gcf)
continue_callback(gcf,event_data,handles)
finish_callback(gcf,event_data,handles)
So which one should be called? Also, since you have already pushed a button, either Continue or Finish, then one of those callbacks will already fire. I think that you can remove these two lines altogether and just keep the
uiwait(gcf)
which will wait until the GUI closes (which you do in both the callbacks).
As an aside,do you want to close the GUI if the user continues? Why not re-use the same GUI for each iteration of the while loop (this would mean you would need to create the GUI outside of the loop)?

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by