Programming a GUI Hanan sedaghat pisheh. For calling GUI, we need a function with no inputs or outputs First We create a m.file m file has the same name.

Post on 26-Dec-2015

220 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

Programming a GUI

Hanan sedaghat pisheh

For calling GUI, we need a function with no inputs or outputs

FirstWe create a m.filem file has the same name as the function

Creating the FunctionCreating the Function

Make sure the function name is the same as the m file’s.

No input and No output

You can use F5 or play You can use F5 or play buttton to run your rogrambuttton to run your rogram

Write figure in your function and run it.

Setting Figure PropertiesSetting Figure Properties

Syntax for setting figure properties is:

figure('PropertyName',propertyvalue,…)

Some Figure PropertiesSome Figure Properties

PropertyName PropertyValue Example

‘name’ string ‘Example’

‘MenuBar’ ‘none’

{‘figure’}‘none’

‘NumberTitle’ {‘on’}‘off’

‘on’

‘color’ [R, G, B] [0.5, 0.9, 0.7]

‘units’ {‘pixels’}‘normalized’

‘inches’

‘centimeters’

‘points’

‘characters’

‘inches’

‘position’ [x, y, w, l] [2, 3, 2, 1]

ColorColor

vector value for [R, G, B], where each vector element takes a value in the range [0, 1].

Examples:[0, 1, 0] = GREEN[1, 1, 0] = YELLOW[1, 1, 1] = WHITE[0.5 0.5 0.5] = GRAY[0.5 0.2 0.75] = PURPLE

PositionPosition

Takes the vector value [x, y, w, l]

Each element is a value in the units specified by the figure property ‘units’

x- Distance from left side of the screen y- Distance from bottom side of the screen w- Width of figure (horizontally) l- Length of figure (vertically)

UnitsUnits

Safest bet is to use the property value ‘normalized’.

With ‘normalized’ chosen, each element is relative to the screen and takes the value in the range of [0, 1].

[1, 1]

[0, 0]

0.45

0.25

0.45

0.45

RANDOM FUNCTIONRANDOM FUNCTION

Creates a random number in the range [0, 1].

Can be used in [R, G, B] vector or even [x, y, w, l] vector

UICONTROLUICONTROL

Creates edit boxes, text boxes, pushbuttons, sliders, popup menus and more.

Syntax:Uicontrol(‘propertyName’,property

value)

UICONTROL Property: UICONTROL Property: ‘‘StyleStyle’’‘style’ property specifies the

UICONTROL type and can have one of the following as its value:

1-pushbutton 7- slider2- toggle button 8- frame3- radio button 9-listbox4-checkbox 10- pop up menu5-edit6-text

UICONTROL Property: UICONTROL Property: PositionPosition‘position’ behaves similarly to the

FIGURE property, only here the position is taken relative to the figure window. Likewise, ‘units’ is treated in the same vein.

[0,0]

UICONTROL PropertiesUICONTROL PropertiesPropertyNa

mePropertyV

alueExample Explanation

‘string’ string ‘Enter Number’

Writes string in UICONTROL

‘foregroundcolor’

[R, G, B] [0, 0, 0] Font colour

‘backgroundcolor’

[R, G, B] [1, 1, 1] UICONTROL colour

‘visible’ {‘on’}‘off’

‘off’ Sets UICONTROL to visible/invisible

‘callback’ String or function handle

@myfunc1 Execute specified function on UICONTROL interaction

Another uicontrolAnother uicontrol

We need to give handles for We need to give handles for objects so we could reference them objects so we could reference them later.later.

important Commandimportant CommandGet and set command are going to use

alot.

Syntax:get(handle,’propertName’)

ExamplesExamples

Imagine we want to use Imagine we want to use the color of figure for the the color of figure for the color of the text color of the text backgroundbackground

So we should get the color of the created figure (with handle “f”)

SET CommandSET Command

Gives pre-existing objects new values for properties.

Syntax:set(handle,’propertyName’,propertyv

alue)

Lets change background color of first text box from green to random.

What if we don’t want a dark background but we still want random one ?

Remember: higher values brighter colors

ANSWER:ANSWER:

rand*0.5+0.5

gives us values in the range [0.5 1].

PushbuttonPushbutton

Pushbutton CallbackPushbutton Callback

pushbutton has the ‘callback’ property name with the property value “@DO1”.

This means that whenever the user interacts with the UICONTROL (in our case push the pushbutton), we will execute the function “DO1”.

““vararginvarargin””stand for variable stand for variable argumentsargumentsThis is due to the fact that the This is due to the fact that the function has no inputs to it.function has no inputs to it.

Lets try this one :Lets try this one :

Add the number in the edit box with 5 and displayed on the text box1

Remember:1-for convert string to numerical value, weuse the command STR2NUM2-use string in set command

PopupmenuPopupmenu

list of possibilities which user can choose from them.

Lets try this one:Lets try this one:

we want to have a popupmenu string and we want the user to choose between squaring and cubing the value in the edit box.

Thus when the pushbutton is pressed, the popupmenu value is checked and based on it, the operation performed will be different.

What do you think?What do you think?

Assume we want to have same example as before but this time in addition of pushing button we want an update when user changes the popupmenu

This means we should give the

popupmenu a callback function

which is the same as pushbutton

callback function.

ListboxListbox

Its function is very similar to the popupmenu.

WELCOME TO second SESSION

LETS HAVE QUICK REVIEWLETS HAVE QUICK REVIEW

SliderSliderImportant slider properties include MIN, MAX, VALUE and SLIDERSTEP

properties.

MIN and MAX properties take the values of the minimum and maximum of the slider, respectively.

SLIDERSTEP is a two element vector that controls how much the slider VALUE changes with each click.

The first value of SLIDERSTEP controls how much the slider moves when one of the arrows on its far sides is clicked.

The second element governs the change of slider value when clicking on an empty space in the slider itself.

Both SLIDERSTEP element are scaled to the MIN to MAX scale, meaning they are to be in the range

[0, 1].

Using the slider to its full Using the slider to its full potentialpotentialWhat we want to do is make the slider control

the number to be inputted.To do this, we give the slider a callback.

When interacting with the slider, its value will be placed in the edit box.

Or using another function to Or using another function to squre it:squre it:

What if we want the edit box to control the slider?

Just like any other UICONTROL, the edit box can be given a callback as well.

In the edit box callback, we change the value of the slider accordingly

What do you think?What do you think?

Remember how we have set the MAX property of the SLIDER to 10?

Well what if this value is exceeded in the edit box, what should happen?

If the value to be set in the slider is higher than its MAX property or lower than it MIN property, then the SLIDER wouldn’t be created.

A solution to this problem is to create an error dialog box whenever this happens, and then proceed to reset the slider.

ERRORDLG FunctionERRORDLG FunctionThe function ERRORDLG produces an

error dialog box with two input parameters.

The second input is the box title, while the first is the error message itself.

Syntax:

errordlg(title,message)

ExampleExample

Checkbox UICONTROLCheckbox UICONTROLThe checkbox UICONTROL is similar to

the listbox and popupmenu in which it has a defining VALUE property.

The value for each checkbox can either be “1” (i.e. checked) or “0”.

Another handy property is the ‘string’ property.

RadiobuttonRadiobutton

The radiobutton is very similar to the checkbox.

You can create a radiobutton group. That way, only one of the buttons in the group can be selected “on” (i.e. has the value of “1”)

Events

• activating the key board

>> a = waitforbuttonpress

In response, Matlab displays a figure window.

when you place the cursor inside the window and press down the mouse, the variable is set to 0 and the window is minimized.

instead, if you press a key, a is set to 1, and you can also extract that key using

>> get(gcf, ‘CurrentChar’)

Menus and other choices

items = {‘Car’, ‘Truck’, ‘Motorcycle’, ‘delete’, ‘QUIT’};

menu (‘Next vehicle:’, items)

Clicking on car returns 1, truck 2 etc.

QUIT closes the menu.

questdlg(‘Does this make sense?’, ‘A question for you’)

generates the following dialog box:

Clicking on Yes (No) generates a string ‘Yes’ (‘No’).

Text Box inputs

Here is a way to create a text box:

Suppose the user enters the information as follows:

Then, a becomes:

a =

'Joe' '1/1/2010''8'5''

Input from mouse

You can use ginput function to get the position of mouse.

Refrences:Refrences:

Husam Aldahiyat ‘s power pointDr. ravikumar ‘s power points

top related