Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.

Similar presentations


Presentation on theme: "MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics."— Presentation transcript:

1 MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics

2 MATLAB and SimulinkLecture 62 Graphical User Interface (GUI) Can make programs easier to use by providing them with: Pushbuttons List boxes Sliders Menus Etc

3 MATLAB and SimulinkLecture 63 Graphical User Interface (GUI) The GUI should behave in an understandable and predictable manner. GUI’s are event driven A event could be a mouse click or a key press.

4 MATLAB and SimulinkLecture 64 Graphical User Interface (GUI) Three principal elements required to create a MATLAB GUI: 1. Components.Each item in a GUI is a graphical component. Controls: Pushbuttons, edit boxes, sliders etc. Static elements: Frames and text strings Menus Axes

5 MATLAB and SimulinkLecture 65 Graphical User Interface (GUI) 2. Figures: The components must be within a figure window. 3. Callbacks: The GUI must perform some kind of action if a user clicks the mouse on a button or the keyboard. (event) The code executed in response to an event is known as a callback. There must be a callback to each graphical component on the GUI.

6 MATLAB and SimulinkLecture 66 Graphical User Interface (GUI)

7 MATLAB and SimulinkLecture 67 Graphical User Interface (GUI) The basic steps to create a MATLAB GUI 1. Decide what elements are required and what function of each element. Draw a rough layout on paper. 2. Use the MATLAB tool: Graphical user interface development tool, guide. To layout all components on a figure. 3. Use a MATALB tool called: Property Inspector to set a name (a tag), color etc.

8 MATLAB and SimulinkLecture 68 Graphical User Interface (GUI) 4. Save the figure to a file. When the figure is saved, two files will be created on the disk with the same name but with different extension. filename.fig and filename.m The fig contains the created GUI. The m-file contains the code to load the figure and skeleton callbacks for each GUI element. 5. Write code to implement the behavior associated with each callback function

9 MATLAB and SimulinkLecture 69 Graphical User Interface (GUI) GUIDE

10 MATLAB and SimulinkLecture 610 Graphical User Interface (GUI) Property inspector Property names and corresponding property values. These values can be set in the property inspector. When the program is running we can still change these values by using the set command. …and view the values by using the get command

11 MATLAB and SimulinkLecture 611 Graphical User Interface (GUI)

12 MATLAB and SimulinkLecture 612 Graphical User Interface (GUI) Pushbutton Create a GUI with one pushbutton and a text field. The program shall count the number of times the user clicks on the button. Display the number of clicks in the text field.

13 MATLAB and SimulinkLecture 613 Graphical User Interface (GUI) In the GUIDE we start by creating a pushbutton. In the property inspector: String:ClickButton Tag:ClickButton We must also create a text field. In the property inspector: String:Number of clicks = 0 Tag:Clicks We save the GUI as Pbutton.fig This is our figure that we have created in the GUIDE. MATLAB will automatically create an m-file: Pbutton.m This file contains skeleton callback functions to the graphical objects.

14 MATLAB and SimulinkLecture 614 Graphical User Interface (GUI) The callback function will have the same name as the tag. function tag_Callback(hObject, eventdata, handles) All callback functions have handles to all graphical object as an input argument. handles is a structure with all graphical handles. We can access all objects from a callback function

15 MATLAB and SimulinkLecture 615 Graphical User Interface (GUI) Toggle buttons Every time a user clicks on it a callback is generated. Two states On = 1 Off = 0 Val=get(handles.togglebutton,’Value’) Radio buttons/Check boxes Every time a user clicks on it a callback is generated. Two states On = 1 Off = 0 Val=get(handles. radiobutton,’Value’)

16 MATLAB and SimulinkLecture 616 Graphical User Interface (GUI) Text fields Graphical object that displays a text string Text fields do not create callbacks. Can be changed in other callback functions by using: Set(handles.text,’String’,str) Edit boxes Allows a user to enter a text string Generates a callback when the user presses the Enter Key sentence=get(handles.edit,'String');

17 MATLAB and SimulinkLecture 617 Graphical User Interface (GUI) Example: Let a user write a sentence in an edit box. Rewrite the sentence backwards in a text field

18 MATLAB and SimulinkLecture 618 Graphical User Interface (GUI) Frames Used for drawing boxes around logically related objects Frames do not generate callbacks

19 MATLAB and SimulinkLecture 619 Graphical User Interface (GUI) Popup Menus Allows a user to select one of a mutually exclusive list of options The list of options is specified by a cell array of strings Example: Create a popup menu with three options. Display the users choice in a text field

20 MATLAB and SimulinkLecture 620 Graphical User Interface (GUI) List boxes Functionality as popup menus The difference between them is that a user can choose more then one line. To enable multiple line choices set the max-min difference greater then 1 in the property editor.

21 MATLAB and SimulinkLecture 621 Graphical User Interface (GUI) Sliders Allows users to select values from a continues range. Generate callbacks Example: Construct a GUI with a slider. Display actual value in a text field.

22 MATLAB and SimulinkLecture 622 Graphical User Interface (GUI) Dialog boxes A special type of figure Used to display information or to get information from the user. Errordlg Helpdlg Inputdlg Etc

23 MATLAB and SimulinkLecture 623 Graphical User Interface (GUI) Menus Use the menu editor in GUIDE Menus generate callbacks

24 MATLAB and SimulinkLecture 624 Exercises on this days topics Create a GUI that plots the water level in the water tanks. We will use the differential equation from the tank process. Create a GUI with: A start button. Axes that are plotting the levels Check boxes so that the user could chose which tank he/she wants to see. Add more features if you like.

25 MATLAB and SimulinkLecture 625 Exercises on this days topics function xprim=TankODE(t,x) A=2734e-6; a=7e-6; km=2.7e-6; g=9.82; alfa=a/A; beta=km/A; xprim = [(beta*5-alfa*sqrt(x(1)*2*g)) alfa*sqrt(x(1)*2*g)-alfa*sqrt(x(2)*2*g)]; Return To solve the equation [time,x]=ode45(@TankODE,[0 500],[0 0]);


Download ppt "MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics."

Similar presentations


Ads by Google