How to check if a figure is closed using the big red 'X' mark?

15 vues (au cours des 30 derniers jours)
Udit Gupta
Udit Gupta le 4 Fév 2015
Commenté : Udit Gupta le 4 Fév 2015
Hi! I am using a gui in my application. There are 'Ok' and 'Cancel' buttons there which upon clicking close the figure and do some additional processing before returning control to the main matlab program.
My query is how do I perform error checking if the user instead of pressing either of the two buttons, chooses to close the gui window using the window close button (big red X in windows)?
I read the documentation about CloseRequestFcn but was unable to figure it out from there.

Réponse acceptée

Image Analyst
Image Analyst le 4 Fév 2015
Do you want them to do the OK button or the Cancel button if they do that? Which ever it is, just call the OK or Cancel callback from the closerequestfcn.
  3 commentaires
Image Analyst
Image Analyst le 4 Fév 2015
No. But if you want, you can also shutdown from that function by deleting the handle. See this example:
% --- Executes when user attempts to close figMainWindow.
function figMainWindow_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figMainWindow (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
global vidobj; % Video camera object.
try % Save the current settings to our disk file.
SaveUserSettings(handles, true);
% Shut down the timer.
StopTimer(handles);
% Shut down the camera
try
delete(vidobj);
catch
end
% Get rid of variables from the global workspace so that they aren't
% hanging around the next time we try to run this m-file.
clear global;
delete(hObject);
catch ME
warningMessage = sprintf('Error in function figMainWindow_CloseRequestFcn.\nError Message:\n%s', ME.message);
set(handles.txtInfo, 'string', warningMessage);
WarnUser(warningMessage);
end
fprintf(1, '\n================= Exited figMainWindow_CloseRequestFcn. =================\n');
return; % from figMainWindow_CloseRequestFcn()
Udit Gupta
Udit Gupta le 4 Fév 2015
OK! Now I see what's happening. Thanks for the info.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by