MATLAB: How to disable the Close button at the top right corner of figure window and the Close option in the title bar menu in MATLAB 7.6 (R2008a)

buttonclosecloserequestfcndisableMATLAB

I want to disable the Close button on the upper left corner of a figure window and the Close menu item in the Title bar menu in a GUI application.

Best Answer

In MATLAB 7.6 (R2008a), the Close button on the upper left corner of a figure window and the Close menu item in the Title bar menu in a GUI application can be disabled by implementing a custom CloseRequestFcn in your GUI. To edit this function, right click on the Figure window in design mode and under the Callback functions portion select the CloseRequestFcn to include this function in the corresponding program file.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (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
delete(hObject);
commenting the following line will prevent the CloseRequestFcn from deleting the figure when a user clicks on the Close button or uses the Close menu item.
delete(hObject)
Please note that with the above setting you have to use the following command to forcefully close the window:
close force