Wednesday, January 6, 2016

Create Simple GUI in Matlab

It's always easier if you can put your Matlab coding in GUI. Especially if you have lot of functions, you can use different push-buttons for each functions. This post will guide you to create a simple GUI, consists of your program title, push-button for function, axes to display your simulation/results.

Steps:

1. In Matlab Command Window
>> guide
choose 'Blank  GUI (default)
2. A window will pop-out, you can design your GUI anyway you like. I usually keep it simple. There are 3 basic things you need for a basic GUI:

  • Static text - optional but I like to have title for all my programs (double-click to access the properties)
  • Push button - I used 2 push buttons, for 'Run' and 'Stop' (double-click to access the properties)
  • Axes - this is where your simulation shown. You can set your Axes to have toolbar
Some note on Axes: I usually have toolbar for Axes, so I can navigate to each pixels and get their information, or zoom in and out. 
To customise your Axes toolbar:
  1. Single left-click on Axes 
  2. Click 'Tools' on the menu
  3. Select 'Toolbar Editor'
  4. Drag the following to the top: Pan, zoom in, zoom out, data cursor (you can customise to your liking)
Building Matlab simple GUI

3. Rearrange and resize your window GUI accordingly. Save your GUI.

4. Once you save your gui (.fig file) an m-file will be automatically generated (the .fig and .m file will have the same name)

5. Open the GUI's m-file, and copy-paste each m-files function to each designated buttons. For instance, the 'Run' button is  under function pushbutton1_callback(hObject, eventdata, handles). I can put all my code under this button.

The 'Stop' button is under function pushbutton2_Callback(hObject, eventdata, handles). I already set my 'Run' button to execute a while loop (eg: while stop==0), so I can set my 'Stop' button as following.

push-button 'Stop'

Basic GUI

No comments:

Post a Comment