How to Close a GUI in MATLAB

Written by sean hill
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Close a GUI in MATLAB
Learn how to create a close confirmation function for your Matlab GUI. (Photos.com/AbleStock.com/Getty Images)

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 customise 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.

Skill level:
Easy

Other People Are Reading

Things you need

  • Matlab R2007a or later version

Show MoreHide

Instructions

  1. 1

    Open Matlab and type "guide" in the command window.

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

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

Tips and warnings

  • Make sure to put a semicolon where one was indicated in the steps above. Otherwise, errors will result.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.