GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this...

67
1 development with Matlab: GUI Front Panel Components GUI front panel components In this section, we will look at - GUI front panel components - Programming the components

Transcript of GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this...

Page 1: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

1

GUI development with Matlab: GUI Front Panel Components

GUI front panel components

In this section, we will look at

- GUI front panel components

- Programming the components

Page 2: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

2

GUI development with Matlab: GUI Front Panel Components

Classes of GUI components

1. Analog input

2. Discrete input

3. Display output

4. Groups

Page 3: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

3

GUI development with Matlab: GUI Front Panel Components

Analog input

An analog input component receives user-defined choices that are analog

The GUI components in this class are

- Edit text

- Slider

Page 4: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

4

GUI development with Matlab: GUI Front Panel Components

Discrete input

A discrete input component receives user-defined choices that are discrete (clear cut choices)

Page 5: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

5

GUI development with Matlab: GUI Front Panel Components

Discrete input

Discrete input components

- Radio button

- Checkbox

- Popup menu

- Listbox

- Push button

- Toggle button

Page 6: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

6

GUI development with Matlab: GUI Front Panel Components

Display output

A display output component is used to show the result of the GUI calculation/processing

The GUI components in this class are

- Axes

- Static text

They have no Callbacks

Page 7: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

7

GUI development with Matlab: GUI Front Panel Components

Groups

A groups component is used to put several components together as ‘one package’

GUI components in this class

- Panel

- Button group

Page 8: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

8

GUI development with Matlab: GUI Front Panel Components

Example 1

Create a GUI that says ‘Hello’ when you press a Push button.

Page 9: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

9

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- Start GUIDE

- Drag and drop a Push Button to the front panel

Page 10: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

10

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

Page 11: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

11

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- Double click on the Push Button to change its properties (String and Tag)

Page 12: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

12

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

The ‘String’ field is what will be seen by the GUI user.You can change it to e.g. ‘Say Hello’

The ‘Tag’ is the name that Matlab recognizes for this component.It is recommended that you change it to e.g. ‘PB_hello’

Page 13: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

13

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- You’ve finished designing the front panel.

- Now program the Callback (in an mfile).

Page 14: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

14

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

Press this to open the ‘mfile editor’ and program the Callback.

PS – be prepared to give it a filename to save

Page 15: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

15

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- Now look for a function ‘PB_hello_Callback’.The mfile automatically creates a function using the name that you gave the component (PB_hello) and adds ‘Callback’ behind it.

- Under the function, write the commands that you want it to execute.

Page 16: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

16

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

This is the command that you have typed

Page 17: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

17

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- Now you’re ready to go!

- Execute the GUI

Press to execute

Page 18: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

18

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- It will appear as a Figure.

Page 19: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

19

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- The result

Displays ‘Hello’ as commanded

Page 20: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

20

GUI development with Matlab: GUI Front Panel Components

Example 1 (solution)

- The result

Displays ‘Hello’ as commanded

Page 21: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

21

GUI development with Matlab: GUI Front Panel Components

You have just seen …

- How to design the front panel

- The use of a Tag and String

- How to program a Callback

- The process of creating and executing a simple GUI

Now we’ll look at the how to program each GUI component, and the syntax

Page 22: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

22

GUI development with Matlab: GUI Front Panel Components

Edit Text

A component that allows the user to enter text – string or number.

Page 23: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

23

GUI development with Matlab: GUI Front Panel Components

>>K=get(hObject,'string');

If you entered ‘abc’ into the Edit Text, then the command above will make K = ‘abc’

Edit Text (Callback)

Retrieves the ‘string’ entered by user. This is a standard command.

A variable. You can call it anything.

Page 24: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

24

GUI development with Matlab: GUI Front Panel Components

At this point, K is a string. If the user enters a number, it will still be a string. To change it to a number,

>>K_num=str2double(K);

Edit Text (Callback)

Another variable that you can call anything.

Page 25: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

25

GUI development with Matlab: GUI Front Panel Components

To pass the variable K for processing (to another Callback), type

>>handles.K=K;

>>guidata(hObject,handles);

Edit Text (Callback)

Another variable. But must be in the format ‘handles.anyname’

Standard command line used to ‘save’ all handles information.

Page 26: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

26

GUI development with Matlab: GUI Front Panel Components

Slider

A component that allows the user to select a value from the slider, between the pre-specified minimum and maximum.

Page 27: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

27

GUI development with Matlab: GUI Front Panel Components

Slider

The maximum and minimum values of the slider can be set in the Property Inspector.

Page 28: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

28

GUI development with Matlab: GUI Front Panel Components

>>K=get(hObject,'value');

>>handles.K=K;

>>guidata(hObject,handles);

Slider (Callback)

This is a standard command. A slider will always return a numerical value, that’s why the label ‘value’.

Reminder: These commands are used if you want the value of ‘K’ to be passed to other Callbacks for further processing.

Page 29: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

29

GUI development with Matlab: GUI Front Panel Components

Radio button

A radio button acts as an option to be chosen by the user. The user clicks on the button the make the choice.

Page 30: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

30

GUI development with Matlab: GUI Front Panel Components

>>K=get(hObject,'value');

Radio button (Callback)

‘value’ will be equal to 1 if the user selects the radio button. Otherwise it will be 0.

Page 31: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

31

GUI development with Matlab: GUI Front Panel Components

Checkbox

Identical to the Radiobutton, except in a different form.

Page 32: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

32

GUI development with Matlab: GUI Front Panel Components

>>K=get(hObject,'value');

Checkbox (Callback)

‘value’ will be equal to 1 if the user selects the checkbox. Otherwise it will be 0.

Page 33: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

33

GUI development with Matlab: GUI Front Panel Components

Pop-up menu

A pop-up menu, when clicked, displays a list of options for the user to choose from.

Page 34: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

34

GUI development with Matlab: GUI Front Panel Components

Pop-up menu

To generate the list of options for the user

1. Press this button in the ‘String’ field

2. And this window will appear

Page 35: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

35

GUI development with Matlab: GUI Front Panel Components

Pop-up menu

To generate the list of options (cont.)

3. Type in the options, each separated by ‘Enter’

Page 36: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

36

GUI development with Matlab: GUI Front Panel Components

>>K=get(hObject,'value');

Pop-up menu (Callback)

‘value’ will indicate the index of the choice made.Example: If the 2nd choice is made, then ‘value’=2.

Page 37: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

37

GUI development with Matlab: GUI Front Panel Components

Listbox

Identical to the Pop-up menu, except that the options are already visible without being clicked on. (To see options in Pop-up menu, need to click on it first).

Page 38: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

38

GUI development with Matlab: GUI Front Panel Components

Pushbutton

A push-button is one that will execute a series of commands when pushed.

No specific Callback commands. Just type in the commands you want to execute.

Page 39: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

39

GUI development with Matlab: GUI Front Panel Components

Toggle button

Gives a state when pressed.

Page 40: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

40

GUI development with Matlab: GUI Front Panel Components

>>K=get(hObject,'value');

Toggle button (Callback)

‘value’ will be equal to 1 if the toggle button is pressed. Otherwise it will be 0.

Page 41: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

41

GUI development with Matlab: GUI Front Panel Components

Static text

Allows you to type text on your GUI.

Also allows you to output text depending on the user’s actions.

Page 42: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

42

GUI development with Matlab: GUI Front Panel Components

Static text

No Callback generated for Static Text.

To output ‘user-dependent’ text, type

>>set(handles.StaticText,'string',‘Your text’);

Standard command

Standard command

The Tag of the Static Text (you assigned). Format is handles.Tag

Text to output

Page 43: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

43

GUI development with Matlab: GUI Front Panel Components

Axes

Allows the user to plot graphs

Page 44: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

44

GUI development with Matlab: GUI Front Panel Components

Axes

No Callback generated for Axes.

To plot a graph, type

>>axes(handles.axes1);

You have now referred to a specific graph. Now type whatever plotting commands you wish to.

Standard command

The Tag of the axes (you assigned). Format is handles.Tag

Page 45: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

45

GUI development with Matlab: GUI Front Panel Components

Extra: Retrieving data

You can also retrieve data from another component without that Callback sending out the data (see example on Edit Text and Slider)

e.g. – you want to retrieve the data (choice) from a Listbox

Page 46: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

46

GUI development with Matlab: GUI Front Panel Components

Extra: Retrieving data

In the Listbox Callback>>K=get(hObject,'value');

>>K=handles.K;

>>guidata(hObject,handles);

Then in the Pushbutton Callback>>K=handles.K;

>>guidata(hObject,handles);

To send data out

Retrieve data

Page 47: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

47

GUI development with Matlab: GUI Front Panel Components

Extra: Retrieving data

You don’t have to do that!

There is an alternative!

In the Pushbutton Callback>> get(handles.tag, ‘value’);

The tag of the Listbox

The value returned by the Listbox

Page 48: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

48

GUI development with Matlab: GUI Front Panel Components

Extra: Retrieving data

If the component is not activated, the default returned value will be 1

Page 49: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

49

GUI development with Matlab: GUI Front Panel Components

Example 2

Create a GUI that plots the function entered by the user when the user pushes a Push Button

- The user can enter the upper and lower bounds of the x-axis, and the resolution.

- A pop-up menu enables the user to choose the graph colour – blue (default), red, green or yellow.

Page 50: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

50

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

Page 51: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

51

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

Now let’s look at how to program the Callbacks

It can all be done within the Push Button Callback

Page 52: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

52

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>lowbound=get(handles.Edtxt_lowbound,'string');

>>lowbound_no=str2double(lowbound);

Tag of the Edit Text box which enters the lower bound of the x-axis

Retrieve data

Converts the string to a numeric value

Page 53: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

53

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

Do the same for the

- Upper bound

- Interval

- Function entered by the user (but no need to convert to number)

Page 54: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

54

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>for i=1:((upbound_no-lowbound_no)/interval_no)+1;

>> u(i)=lowbound_no+interval_no*(i-1);

>> x=u(i);

>> y(i)=eval(graph_func);

>>end

>>hline=plot(u,y);

Data retrieved by the graph function

Giving the plotted line a name, to set properties later

Page 55: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

55

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>1+1

>>‘1+1’

>> eval(‘1+1’)

>> x=3; eval(‘x+4’)

This will give a number 2

This will give a number 2

This will still give a string ‘1+1’

This will give a number 7s

Page 56: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

56

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>linecolour=get(handles.Popup_colour,'value');

>>linestyle=get(handles.Listbox_style,'value');

Retrieve data on what user specified with regard to line colour and style

Page 57: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

57

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>switch linecolour>> case 1>> set(hline,'color','b');>> case 2>> set(hline,'color','r');>> case 3>> set(hline,'color','y');>>end

This is why we gave the line a name earlier

A property field (standard command) of the line

Page 58: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

58

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>switch linestyle>> case 1>> set(hline,'linestyle','-');>> case 2>> set(hline,'linestyle',':');>> case 3>> set(hline,'linestyle','--');>>end

Page 59: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

59

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

>>grid_on=get(handles.Radio_grid,'value');>>if grid_on==1>> grid on;>>else>> grid off;>>end

If the Radio button is clicked, it will return a value of 1

Page 60: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

60

GUI development with Matlab: GUI Front Panel Components

Example 2 (solution)

Now, test the GUI- Enter the function in terms of ‘x’- Enter the lower bound, upper bound,

and interval- Press the Plot It button- Make other modifications in terms of

line colour, style and grid

Page 61: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

61

GUI development with Matlab: GUI Front Panel Components

Example 3

Create a GUI with the following features- Calculates the volume of a cuboid

when the user enters the height, width and depth

- Outputs the result in a Static Text box

Page 62: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

62

GUI development with Matlab: GUI Front Panel Components

Example 3 (solution)

Tag – Edtxt_height

Tag – Edtxt_width

Tag – Edtxt_depth

Tag – Pb_calculate

Tag – Sttxt_result

Page 63: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

63

GUI development with Matlab: GUI Front Panel Components

Example 3 (solution)

In the Edtxt_height Callback>>height=get(hObject,'String');

>>height_num=str2double(height);

>>handles.height_num=height_num;

>>guidata(hObject, handles);

Page 64: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

64

GUI development with Matlab: GUI Front Panel Components

Example 3 (solution)

In the Edtxt_width Callback>>width=get(hObject,'String');

>>width_num=str2double(width);

>>handles.width_num=width_num;

>>guidata(hObject, handles);

Page 65: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

65

GUI development with Matlab: GUI Front Panel Components

Example 3 (solution)

In the Edtxt_depth Callback>>depth=get(hObject,'String');

>>depth_num=str2double(depth);

>>handles.depth_num=depth_num;

>>guidata(hObject, handles);

Page 66: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

66

GUI development with Matlab: GUI Front Panel Components

Example 3 (solution)

In the PB_calculate Callback

>>depth_num=handles.depth_num;

>>width_num=handles.width_num;

>>height_num=handles.height_num;

>>result=depth_num*width_num*height_num

>>result_str=num2str(result)

>>set(handles.Sttxt_result,'string',result_str)Convert the number to a string first before sending out to the Static Text

Page 67: GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.

67

GUI development with Matlab: GUI Front Panel Components

Conclusion

• Looked at the GUI front-panel buttons• Looked at how to program them• 2 examples for practice