How to Close a GUI in MATLAB

Save

Matlab is a technical computing program for engineers. Matlab includes a tool for building graphical user interfaces (GUIs) that lets users select objects from a palette of pre-coded elements and customize these objects using a property inspector. When it comes time for the user to test and use the GUI, it is important to program a method to close the GUI that ensures the user will not lose any work. This can be done by programming a close confirmation function.

Things You'll Need

  • Matlab R2007a or later version
  • Open Matlab and type "guide" in the command window.

  • Click the "Open Existing GUI" tab when prompted. Navigate to your saved GUI, select it, and click "OK."

  • Click the paper and pencil icon in the GUI editor window to open the m-file.

  • Add the following code under the first function listed on the m-file: "guidata(hObject, handles);"

  • Type the following code under the the line of code you typed in step four: "set(handles.figure1,'CloseRequestFcn',@closeGUI);"

  • Type the following at the end of your m-file: "function closeGUI(src,evnt)"

  • Add this code under your new closeGUI function: "selection = questdlg('Do you want to close the GUI?','Close Request Function','Yes','No','Yes');"

  • Type "switch selection," on the next line of code.

  • Type "case 'Yes'," on the next line of code. Make sure this line is indented once.

  • Type "delete(gcf)" on the next line. Make sure this line is indented twice.

  • Type "case 'No'" on the next line. Make sure this line is indented once.

  • Type "return" on the next line. Make sure this line is indented twice.

  • Type "end" on the next line to finish your new close confirmation function. Save the m-file and you are finished.

References

  • Photo Credit Photos.com/AbleStock.com/Getty Images
Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!