BUILDING A MATLAB GUI Education Transfer Plan Seyyed Khandani, Ph.D. IISME 2014
Graphical User Interface (GUI) A GUI is useful for presenting your final software. It makes adjusting parameters and visualizing your programs easier. It allows wrapping the software in a form that could be used by people with no knowledge of computer programming. It encourages design-oriented mentality which is essential in engineering education. 2
Guide MATLAB provide a tool, called guide, for creating a GUI. Typing guide in the command window opens the GUIDE Quick Start window shown below: >> guide Begin by using Blank GUI (default) template. 3
Guide Window Guide window and blank GUI panel shown below The window has 3 areas: (1) The menu and menu bar on the top. (2) Buttons to select GUI objects, or controls, on the left. (3) The resizable panel with grid marks where GUI objects will be placed. To resize the panel grab the lower right corner with the mouse and stretch. 4
GUI Objects (controls) in Guide Guide GUI objects are instances of a MATLAB class called uicontrol (user interface control) Push button ------ A button to activate by mouse Radio button ----- Changes state from unselected to selected and back Edit textbox ------ Input data by typing text into the box Pop-up menu ---- A list of options to select one Toggle button ---- Pressed down, it stays down until it is pressed again Axes ----------------- A surface on which to display two and three-dimensional graphs Button group ---- Groups a set of radio buttons or toggle buttons; only one at a time is selected Slide ---------------- Enter a real number by adjusting the position of the slider Checkbox --------- Changes state between select and unselected Push button Radio button Edit textbox Pup-up menu Toggle button Static text --------- Displays a text; use for label or show the result of a calculation Listbox ------------- Display a list of options where several can be selected Table ---------------- Display data in a tabular form Panel --------------- Groups objects together visually ActiveX control For insertion of an ActiveX control made by another program Axes Button group Slide Checkbox Static text Listbox Table Panel ActiveX control 5
MyGUI MyGUI is an example of how to create a GUI. First layout the basic controls for our program. It contains Axes(axes1), Static Text, Edit Text, and Push Button controls. Use drag-and-drop to add GUI objects, or controls, to the panel. Resize and move around each object to look like the image on the right. Selected Object Tag Property Mouse Pointer Position Selected Object Position Resize Box 6
GUI Object Property Inspector Each GUI object has a number of properties. Doubleclick on an object to open the property inspector Sample of properties (1) Tag the name of the control in the code; best to rename it to something identifiable like PlotButton versus button1 (2) String the text that appears on the control (3) ForegroundColor color of the text (4) BackgroundColor color of the control (5) Value current value (initial value is zero) (6) Min and Max depends on the type of control. In slider, for example, they show the range of the value 7
Running MyGUI Change properties of different controls: (1) axes1 tag: PlotArea (2) push button tag: PlotButton string: Plot (3) edit text tag: EquationBox string: Equation (4) static text tag: EquationLabel string: y= Save the setup as MyGUI and then run it by pressing on the menu bar. It will look like the image show above It looks right but it does not do anything until callbacks are defined. 8
GUI File Formats When MyGUI was saved, two files were created. (1) MyGUI.fig contains information about the GUI layout, geometry, and its controls. (2) MyGUI.m contains the program that creates and executes the GUI tool you have made. also contains code that defines a callback for each of the controls in GUI. This m-file loads in the MATLAB editor when MyGUI is first saved. Generally the content of the m-file is left alone except for some callback functions. In the m-file, we will modify a callback function called PlotButton_callback. 9
MyGUI.m File Content MyGUI.m contains a number of functions including: (1) function MyGUI_OpeningFcn(hObject, eventdata, handles, varargin) - This opening function, executes just before MyGUI is made visible. - The function has no output. - Input argument handles is a structure containing handles for all GUI controls such as handles.equationbox, and handles.plotarea. - This function will not be modified (2) function PlotButton_Callback(hObject, eventdata, handles) - This is the callback function for plot button. - The function has no output. - Entering an equation in the Equation box followed by a click on the Plot button, should result in display of the plot of that function in the plot area. Therefore this function must be modified. 10
Writing Callbacks Since the only part to be modified is PlotButton_Callback function. Then add the following code to the end of this function % added code to MyGUI.m x=-10:0.1:10; s=get(handles.equationbox, 'String'); y=eval(s); h=handles.plotarea; plot(h,x,y); xlabel(h,'x-value'); ylabel(h,'y-value'); title(h,'plot of y versus x'); Save MyGUI.m once % an arbitrary range for x % gets the equation entered % eval function finds values of y % records the handle of where % the plot is displayed % plots on the PlotArea 11
Running MyGUI When the m-file code is modified, there is no need to rerun the GUI. The new code will run as a callback allowing a quick testing of the changes made. To run the GUI, the only thing to do is to enter MyGUI in the MATLAB command window: >> MyGUI As a result of running MyGUI.m, the corresponding GUI opens. First enter your function in the Equation box and then click on the Plot button to see the results. 12
Test Results for MyGUI Inputs: y=sin(x), or y=4*x.^3+2*x-1, or y=x.*sin(x) 13
Usefull MATLAB Functions get This function is used to get the properties of a graphic (e.g. gui) object. <var>=get(handle, PropertyName ) where handle is a handle to the object and var represent the value of the property listed set This function sets the value of a property of a graphic (e.g. gui) object. findobj set(handle, PropertyName, property_value) Finds objects with specified property values. handles = findobj('p1name',p1value,...) returns the handles of all objects whose property values match those passed as param-value pairs to the findobj command. 14