sbt 645 introduction to scientific computing in sports...

31
1 SERDAR ARITAN [email protected] Biyomekanik Araştırma Grubu www.biomech.hacettepe.edu.tr Spor Bilimleri Fakültesi www.sbt.hacettepe.edu.tr Hacettepe Universitesi, Ankara, Türkiye www.hacettepe.edu.tr De Motu Animalium G.Borelli (1680) SBT 645 Introduction to Scientific Computing in Sports Science #5

Upload: others

Post on 09-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

1

SERDAR ARITAN [email protected]

Biyomekanik Araştırma Grubu www.biomech.hacettepe.edu.tr Spor Bilimleri Fakültesi www.sbt.hacettepe.edu.tr Hacettepe Universitesi, Ankara, Türkiye www.hacettepe.edu.tr

De Motu Animalium G.Borelli (1680)

SBT 645 Introduction to Scientific Computing in Sports Science

#5

Page 2: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

2

Graphical User Interface (GUI)

The graphical user interface development environment

(GUIDE) enables the rapid creation of a graphical user

interface (GUI) for a MATLAB program The GUI is contained in

two files, a figure file with the suffix .fig, which contains

graphical layout information, and an m-file with suffix .m,

which contains the main GUI function and a number of

subfunctions. This latter file is written in template form by

GUIDE, and can be edited to add functionality and connect

with other functions.

>> guide % start MATLAB GUI developer

Page 3: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

3

Graphical User Interface (GUI)

Page 4: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

4

Graphical User Interface (GUI)

Page 5: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

5

Graphical User Interface (GUI)

Push button

Radio button

Edit textbox

Pop-up menu

Togglebutton

Axes

Button group

Slider

Checkbox

Static textbox

Listbox

Table

Panel

ActiveX control

Page 6: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

6

Graphical User Interface (GUI)

The GUI objects you can create using the buttons on the left

panel of GUIDE are:

• push button – A button that activates when the user clicks

the mouse on it.

• slider – Enter a real number by adjusting the position of the

slide.

• radio button – Changes its state from unselected to selected

and back.

• checkbox – Changes state from selected (checked) to

unselected (unchecked).

• edit textbox – Allows the user to input information by typing

text into the window.

Page 7: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

7

• static textbox – Displays some text; useful for labeling items

or reporting the results of a calculation.

• pop-up menu – Gives the user a list of options from which

one may be selected.

• listbox – Presents a list of options that can be scrolled.

Several items can be selected at the same time.

• toggle button – Pressed once, it stays down until it is pressed

again.

• table – Displays data in a tabular form.

• axes – A surface on which to display two-dimensional and

three-dimensional graphics.

Graphical User Interface (GUI)

Page 8: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

8

Graphical User Interface (GUI)

• panel – Groups controls together visually.

• button group – Groups a set of radio buttons or toggle

buttons so that they act together

with the result that only one at a time is selected.

• activeX control (Windows) – allows the insertion of an

ActiveX control made by another program.

(Only use these if you really know what you’re doing;

improperly configured controls can crash MATLAB.)

Page 9: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

9

Graphical User Interface (GUI)

Page 10: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

10

Graphical User Interface (GUI)

Page 11: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

11

Graphical User Interface (GUI)

Page 12: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

12

Graphical User Interface (GUI)

Page 13: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

13

Graphical User Interface (GUI)

Page 14: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

14

Graphical User Interface (GUI)

Page 15: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

15

Graphical User Interface (GUI)

Page 16: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

16

Graphical User Interface (GUI)

In the MATLAB Editor window, scroll through the

StartTool.m code that GUIDE has generated. It consists of

four functions:

• StartTool – This is the main function; it creates the tool itself.

• StartTool_OpeningFcn – This executes when the function is

started up. This is the place to put many different kinds of

initialization steps. Right now it can be ignored.

• StartTool_OutputFcn – This function is for more advanced

uses. Ignore it now and for a long time.

• goButton_Callback – This function is executed when the

goButton is pressed and released.

Page 17: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

17

Graphical User Interface (GUI)

% --- Executes on button press in goButton.

function goButton_Callback(hObject, eventdata, handles)

% hObject handle to goButton (see GCBO)

% eventdata reserved - to be defined in a future version

of MATLAB

% handles structure with handles and user data (see

GUIDATA)

disp('Go, go, go, world!')

>>

Go, go, go, world!

Page 18: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

18

Graphical User Interface (GUI)

Page 19: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

19

Graphical User Interface (GUI)

Using the Property Inspector, make the following changes to

the objects:

• Set the value of the Tag for the slider to “xSlider.”

• Set the value of the Value property of the slider to 1.0.

Because the Value property for many objects can be a vector,

it is set using a pop-up panel activated by clicking the icon to

the right of the word “Value’ in the property list. This will pop

up the Value Panel, which allows you to edit the value. Click

on the present value (0) and change it to 1; then press OK.

• Change the Max property, which sets the maximum value of

the slider, from 1.0 to 10.

Page 20: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

20

Graphical User Interface (GUI)

• Set the value of the Tag property of the edit text object

to “xText.”

• Set the value of the String property of the textbox to “1.”

• Set the value of the String property of the static text

object to “x[m].” We suppose that we are going to use this

slider to allow the user to set the value of a physical

position x, given in units of meters. The user will be able to

adjust the value of x to be between 0 and 10.

Page 21: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

21

Graphical User Interface (GUI)

Page 22: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

22

Graphical User Interface (GUI)

Page 23: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

23

Graphical User Interface (GUI)

Page 24: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

24

Graphical User Interface (GUI)

In order to communicate with a GUI object, we need to

know the handle of the object. A handle is the address

of the object in the computer’s memory, encoded by

MATLAB as a real number.

>> hslider=findobj('Tag', 'xSlider')

hslider =

UIControl (xSlider) with properties:

Style: 'slider'

String: {'Slider'}

BackgroundColor: [0.9000 0.9000 0.9000]

Callback: [function_handle]

Value: 1.7431

Position: [13.6000 6.6154 3.4000 11.6154]

Units: 'characters'

Show all properties

Page 25: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

25

Graphical User Interface (GUI)

<var>=get(<handle>, <Property name>);

set(<handle>, <Property name>, <Property value>);

Page 26: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

26

Graphical User Interface (GUI)

>> htext=findobj('Tag', 'xText')

htext =

UIControl (xText) with properties:

Style: 'edit'

String: '1'

BackgroundColor: [1 1 1]

Callback: [function_handle]

Value: 0

Position: [9.4000 4.3846 11.2000 1.8462]

Units: 'characters'

Show all properties

>> get(htext, 'FontName')

ans =

MS Sans Serif

>> xfirst=5.23;

>> set(htext, 'String', num2str(xfirst));

Page 27: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

27

Graphical User Interface (GUI) Synchronizing information with a GUI element

In the MATLAB editor. You will see SliderTool-related

functions, two functions associated with the slider and two

functions associated with the textbox.

function xSlider_Callback(hObject, eventdata, handles)

function xSlider_CreateFcn(hObject, eventdata, handles)

function xText_Callback(hObject, eventdata, handles)

function xText_CreateFcn(hObject, eventdata, handles)

Only the callback functions concern us here. Each callback

function has the handles structure passed to it as an

argument. The very important handles structure is

conveniently constructed so that each fieldname in the

structure is the Tag of an object in the GUI

Page 28: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

28

Graphical User Interface (GUI) Synchronizing information with a GUI element

Thus, within the callback function we have access, without the

need of findobj, to the handle for each object in the GUI tool.

For example, in the present case:

handles.xSlider contains the handle to the slider with Tag “xSlider”

handles.xText contains the handle to the textbox with Tag “xText”

1. Get the new value of the slider’s position from itsValue

property. This will be a number we can store in the variable x.

2. Make a string representing the number x.

3. Set the value of the String property of the textbox to this

new string.

Page 29: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

29

Graphical User Interface (GUI) Synchronizing information with a GUI element

function xSlider_Callback(hObject, eventdata, handles)

% GUIDE generated comments omitted

X = get(handles.xSlider, 'Value');

Xs = num2str(x);

set(handles.xText, 'String', xs);

The value of SliderStep is a vector

[smallStepSize, largeStepSize];

its default setting is [0.01, 0.1].

Page 30: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

30

Graphical User Interface (GUI) Synchronizing information with a GUI element

At the moment the user presses <Enter>after typing in the

textbox the function xText_Callback is executed. So when the

callback function for the textbox is executed we’d like it to do

the following:

1. Get the new string from the String property of the textbox.

2. Find the number represented by that string.

3. Set the value of the Value property of the slider to this

updated number.

function xText_Callback(hObject, eventdata, handles)

% GUIDE generated comments omitted

Xs = get(handles.xText, 'String');

X = str2double(xs);

set(handles.xSlider, 'Value', x);

Page 31: SBT 645 Introduction to Scientific Computing in Sports ...yunus.hacettepe.edu.tr/~saritan/sbt645/hafta_05.pdf15 Graphical User Interface (GUI) 16 ... Homework Write a GUI program that

Graphical User Interface (GUI) Homework

Write a GUI program that calculates BMI

31