Error evaluationg 'OpenFcn' because of a waitfor command

Hi,
I have a gui, that when close with a cancle or the X at the top right corner of the gui will give me this error :
The reason why i get this error is because in my program i have a waitfor command that waits for the user to click on pushbutton to allow the rest of the programm to run (that waitfor command is pretty essential for the programm). This error happens if the gui is closed before the pushbutton associated to the waitfor command is pressed.
Is there a way to not have this error message without removing the waitfor ?

 채택된 답변

Rik
Rik 2022년 10월 17일

0 개 추천

You need to use return instead of guidata(hObject,handles) if the figure no longer exists.
If you want more specific advice, you will have to show more specific code.

댓글 수: 6

Sorry for the late answer.
I see what you mean but i don't really understand how to do it. I have set up my pushbuttons this way :
%pushbutton OK
uicontrol(h0,'Style','pushbutton',...
'BackgroundColor',gris,...
'Position',[27 0.5 16 2],...
'String','OK',...
'Tag','NormeOK',...
'Callback',@(o,~) set(o,'UserData',0));
%pushbutton cancel
uicontrol(h0,'Style','pushbutton',...
'BackgroundColor',gris,...
'Callback','close(gcbf)',... %Close the gui when pressed
'Position',[51 0.5 16 2],...
'String','Cancel');
then in another function i call my gui and after some code i will use this line
waitfor(findobj(hf,'Tag','NormeOK'),'UserData') %wait for the user to click on the pushbutton OK
to avoid the problem i tried to set up a while loop, before this command, that would check if the figure still exist while the user didn't click. Do nothing if the figure exist or return if this one no longer exist :
while get(findobj(hf,'Tag','NormeOK'),'UserData')==0 %while the pushbutton was not pressed
if isgraphics(hf) == false %figure no longer exist
return %end the script
else %Do nothing if the figure exist
end
end
But this programme won't work it will either completly skip the loop or once the gui is closed instead of stopping the script it will give me an error message 'Invalid handle'
I think the easiest/best solution would be to use an explicit function instead of the anonymous function for your ok button.
You may also be interested in the drawnow function, which lets you process callbacks before continuing. Some people like this setup:
while get(SomeObject,'Value')
%
% some code that needs to run in a loop
%
drawnow % process the callback that might set the Value of SomeObject
end
thank you for the answer i will try it
Ali
Ali 2022년 10월 18일
편집: Ali 2022년 10월 18일
I tried using a while loop again but i changed some things :
%pushbutton OK
handles.X=0;
handles.button = uicontrol(h0,'Style','pushbutton',...
'BackgroundColor',gris,...
'Position',[27 0.5 16 2],...
'String','OK',...
'Tag','NormeOK',...
'Callback',@buttonCB);
guidata(h0,handles);
%pushbutton cancel
uicontrol(h0,'Style','pushbutton',...
'BackgroundColor',gris,...
'Callback','close(gcbf)',... %Close the gui when pressed
'Position',[51 0.5 16 2],...
'String','Cancel');
I added a function so that when the user press on the pushbutton ok handles.X takes the value 1 :
function buttonCB(ButtonH, EventData)
handles = guidata(ButtonH);
handles.X = 1
guidata(ButtonH, handles)
end
now i put my while loop in my function and my problem is when i use the debugger and go step by step it works perfectly but when the function is launched normaly it will just keep going without stopping and causing the matlab app to crash :
X = 0
while X == 0
if ishghandle(hf) == 1 % if the figure is open do nothing
else % if the figure isn't open end the program
return
end
handles_hf = guidata(hf);
X = handles_hf.X; % X takes the value of handles.X
end
I don't understand why it works with the debugger but doesn't work when the debugger but not when launched normally.
You should put a call to drawnow in your loop. That way, you tell Matlab to execute any queued graphics changes/interactions, including button callbacks.
Calling pause (with some value as an argument) will have a similar effect. The reason it works in the debugger is that every time the code execution stops, callbacks are processed.
ok thank you for the answer

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2012a

질문:

Ali
2022년 10월 17일

댓글:

Ali
2022년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by