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 ?

 Respuesta aceptada

Rik
Rik el 17 de Oct. de 2022
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 comentarios

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
Ali
Ali el 17 de Oct. de 2022
thank you for the answer i will try it
Ali
Ali el 18 de Oct. de 2022
Editada: Ali el 18 de Oct. de 2022
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.
Rik
Rik el 18 de Oct. de 2022
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.
Ali
Ali el 18 de Oct. de 2022
ok thank you for the answer

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Productos

Versión

R2012a

Etiquetas

Preguntada:

Ali
el 17 de Oct. de 2022

Comentada:

Ali
el 18 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by