How to nest callback into while loop?

조회 수: 12(최근 30일)
Sergey Lopatnikov
Sergey Lopatnikov 2021년 10월 11일
댓글: Sergey Lopatnikov 2021년 10월 13일
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
Sergey Lopatnikov
Sergey Lopatnikov 2021년 10월 12일
Thank you. Is it OK now?

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

채택된 답변

Geoff Hayes
Geoff Hayes 2021년 10월 13일
@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)?
  댓글 수: 1
Sergey Lopatnikov
Sergey Lopatnikov 2021년 10월 13일
Thak you great,

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

추가 답변(0개)

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by