Você está na página 1de 39

Building a Graphical User Interface with MATLAB

I-1

Basic Matlab
Matrix definition
a=[1,2 ; 3,4] =>
a= 1 2
34
Vector is a matrix with one column or one row.
a=[1;2;3;4;5 ;; ] one column
b=[1,2,3,4,5,6 ,,..] one row

Building a Graphical User Interface with MATLAB

I-2

Basic Matlab
Example
a=[1,2,3,4,5];
b=[a;a;a]

Example
a=[1;2;3;4;5];
b=[a,a,a]

b=

b=

12345
12345
12345

Example (transpose)
a=[1;2;3;4;5];
b=a
b= 1 2 3 4 5

1111
2222
3333
4444
5555

Building a Graphical User Interface with MATLAB

Basic Matlab
Strings
a=[a,b,c]
a= abc

Strings
a=[a,b,c]
a= abc

Strings
a=[a,b,c]
b=[a,c,c]

b=[a;a;a]

b=[a,a,a]

c=(a==b);

b=

b=

c= 1 0 1

abc
abc
abc

abcabcabc

I-3

Building a Graphical User Interface with MATLAB

I-4

Basic Matlab
1 2
A

3 4
5 6
B

7 8
6 8
A B

10 12
19 22
A* B

43
50

5 12
A. * B

21
32

1 4
A. ^ 2

9 16
sin(1) sin(2)
sin( A)

sin(3) sin(4)

Building a Graphical User Interface with MATLAB

I-5

Basic Matlab
A=zeros(a1, a2, a3, an);
A is an n dimensional matrix of zeros.
A=ones(a1, a2, a3, an);
A is an n dimensional matrix of ones.
size(A) return the size of A at each diminution.
size(A,Dim) return the size of A at the diminution Dim.

Building a Graphical User Interface with MATLAB

Basic Matlab
A(m,n) returns the value of the matrix in row-m
and column-n.
b=A(1:end,1)

: b will be equal to column 1 of A

b=A(5:10,5:10) : b will a-6x6 matrix containing all


values of A from rows 5-10 and columns 5-10.

I-6

Building a Graphical User Interface with MATLAB

Basic Matlab
Functions in Matlab
function [output variables]=function_name (input variables)

Input and output variables, can be of any type.

I-7

Building a Graphical User Interface with MATLAB

I-8

Basic Matlab
Functions in Matlab
function [out_1,out_2,out_3] = Function_Dec (in_1,in_2,in_3)
out_1=in_1+in_2+in_3;
out_2=[ 'hello' ; 'world' ];
out_3=[1,2,3,4,5];
return;

Building a Graphical User Interface with MATLAB

I-9

Basic Matlab
Functions in Matlab
input:
[a,b,c]=Function_dec(5,3,2)

input:
[a,b,c]=Function_dec([1,2,3],[6,5,4],[3,4,5])

output:
a = 10
b = hello
world

output:
a = 10 11 12
b=
hello
world

c=

c=1

Building a Graphical User Interface with MATLAB

Basic Matlab

Bit-wise operations

Variables must be integers


BITAND
BITOR
BITXOR
BITGET
BITSET
BITSHIFT

(a,b)
(a,b)
(a,b)
(a,bit-num)
(a,bit-num,1/0)
(a,+/- shift_size)

Bit-wise AND.
Bit-wise OR.
Bit-wise XOR.
Get bit.
Set bit.
Bit-wise shift.

I - 10

Building a Graphical User Interface with MATLAB

Basic Matlab
conditions
If ( Boolean expression)
.
end;
Boolean expression
== : Is Equal
~=: Not Equal
> : Is grater then
< : Is less Then
>=: Is grater then or equal to
<=: Is less then or equal to

I - 11

Building a Graphical User Interface with MATLAB

Basic Matlab
conditions
switch switch_expr
case case_expr,
statement, ..., statement
case {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
otherwise,
statement, ..., statement

end

I - 12

Building a Graphical User Interface with MATLAB

I - 13

Basic Matlab
Loops
for j=start:step:end
.
end;
example:
for j=-1:0.2:3
.
end;

while Boolean expression,


.
end;
example:
while a>b,
.
end;

break - Terminate execution of WHILE or FOR loop.


break terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.

Building a Graphical User Interface with MATLAB

Basic Matlab
Drawing

plot(X,Y) plots vector Y versus vector X


y
m
c
r
g
b
w
k

yellow
magenta
cyan
red
green
blue
white
black

.
o
x
+
*
s
d
v
^
<
>
p
h

point
circle
x-mark
plus
star
square
diamond
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram

- solid
: dotted
-. dashdot
-- dashed

I - 14

Building a Graphical User Interface with MATLAB

Basic Matlab
Drawing
Drawing a circle
t=0:0.1:2*pi;

Drawing a doted circle.


t=0:0.1:2*pi;

x=10*cos(t);
y=10*sin(t);
plot (x,y);

x=10*cos(t);
y=10*sin(t);
plot (x,y,.);

I - 15

Building a Graphical User Interface with MATLAB

I - 16

Basic Matlab
Drawing
ezplot(x^2+y^2=1);

ezplot(x^2/5+y^2/20=1);

Building a Graphical User Interface with MATLAB

Basic Matlab
Drawing
a=0:0.2:2*pi;
b=ones(1,length(a));
c=sin(a'*b);
figure;
subplot(1,2,1);
surf(c);

I - 17

Building a Graphical User Interface with MATLAB

Basic Matlab
Drawing
figure;
t=0:0.1:2*pi+0.1;
b=ones(1,length(t));
z=b'*(1:1:length(t));
x=(10*sin(z)+15).*sin(t'*b);
y=(10*sin(z)+15).*cos(t'*b);
surf(x,y,z);

I - 18

Building a Graphical User Interface with MATLAB

I - 19

Basic Matlab
Some useful commands
figure
drawnow
hold on

-open new figure.


-draw to screen immediately.
-draw the next draw to the same figure and axes.
-Transpose
- remark

help command- return the help information on the command.


lookfor Word - return a list of all commands that have
the desired word in their help.

Building a Graphical User Interface with MATLAB

Basic Matlab
Adding a path to a library
1

I - 20

Building a Graphical User Interface with MATLAB

I - 21

Adding a path to a library


8
5

Building a Graphical User Interface with MATLAB

Basic Matlab
Adding a path to a library
9

10

I - 22

Building GUIs with MATLAB

GUIs

Oren Meirom
Omikron Delta

Building a Graphical User Interface with MATLAB

I - 24

What is a Callback?

A callback is a sequence of commands which are


implemented by activating a graphics object:
e.g. CreateFcn, CallBack, ButtonDownFcn, DeleteFcn.

Building a Graphical User Interface with MATLAB

The 10 styles of Matlab Uicontrol objects


Push Button.
Toggle Button.
Check Box.
Radio Button.
Editable Text.
List Box.
Pop-up Menu.
Slider.
Frame.
Static Text

I - 26

Building a Graphical User Interface with MATLAB

I - 27

Push/Toggle Buttons
The push button is widely prevalent uicontrol
style that is used primarily to indicate that a
desired action should immediately take place.
The toggle button look just like push button,
except there is no intermediate state.Rather, the
button will remain in its selected or not selected
state after the user clicks on it.

>>mcpush

Building a Graphical User Interface with MATLAB

Check Box
Useful for representing two states of an option
that you may want to provide (usually as on and
off).
In its off state the check box will consist of an
empty or unfilled square. In the on state, the
check boxs square will contain a V sign.

>>mccheckbox,mccheckbox1

I - 28

Building a Graphical User Interface with MATLAB

I - 29

Radio Button
Similar to the check box in that there are two
states associated with each other.
Usually two or more radio buttons are linked
together as a group.They are linked in the sense
that only one of the buttons will be in its selected
state.

>>mcradiox

Building a Graphical User Interface with MATLAB

I - 30

Editable Text
Used in situations that require the user to enter
strings or characters or numbers. The strings , in
turn, are used by the application for which the
interface has been built.
Clicking anywhere within this object will change
the mouse from a pointer to a text insertion
indicator.

>>mcedit, mceditf

Building a Graphical User Interface with MATLAB

I - 31

List Boxes
New style provided by MATLAB 5.x
Very similar to pop-up menus.
>>mccheckboxwith a list box is that you
The main difference
can make the set of options visible to the user at
all times.

>>mclistbox

Building a Graphical User Interface with MATLAB

I - 32

Pop-up Menus
Used in situations where multiple choices need to
be available to the user.
When the user clicks and holds the mouse button
anywhere within the object, a list of choices
appear.

>>mcpopup

Building a Graphical User Interface with MATLAB

I - 33

Sliders
Useful in representing a fixed range of values
from which to choose.
The slider has no way of explicitly indicating the
numeric value that the slider represents.
Therefor, it is recommended that an editable text
or static text style uicontrol accompany the
slider.

>>mcslider, mcslider2

Building a Graphical User Interface with MATLAB

I - 34

Frames
Provide a solid background that helps blend a set
of uicontrols into one complete and cohesive
interface.
Used as an effective method of organizing the
GUI in a logical and intuitive fashion.

Building a Graphical User Interface with MATLAB

Static Text
Available for creating labels, status messages or
other information pertinent to the user.
Static text does not perform any action if the user
clicks on any part of the object. In addition , the
user can not edit the information that is displayed.

I - 35

Building a Graphical User Interface with MATLAB

I - 36

Properties that Track User Actions


ButtonDownFcn- When clicking the mouse button while the pointer is located over or near the object.
WindowButtonDownFcn- When clicking the mouse button down within the figure
boundaries.

WindowButtonUpFcn- When clicking the mouse button up within the figure boundaries.
WindowButtonMotionFcn- When the mouse pointer moves within the figure boundaries.
KeyboardFcn- When the figure is active.
CreatFcn- When creating an object.
DeleteFcn- When deleting an object.
ResizeFcn- When resizing the figure.

Building a Graphical User Interface with MATLAB

I - 37

Some useful instructions.


AXES(axes_handle) - make the axes, current.

Object_H=GCBO

- to get the object that make


the callback.

RBUTTON(Radio_H) - use to select and deselect


radio buttons with the
same Tag Name

Building a Graphical User Interface with MATLAB

I - 38

Setting figure.
h0 = figure('Color',[0.8 0.8 0.8], ...
'Units','Normal', ...
'Position',[0.3 0.3 0.3 0.3], ...
'Tag','Fig1');
%Setting figure name
set(h0,'Name','Check and radio');

Building a Graphical User Interface with MATLAB

I - 39

Setting Push Button.


h1 =uicontrol ('Parent',h0, 'Units','Normal','Position',[0.1
0.3 0.3 0.4] ,'Tag','Pushbutton1');
%Setting the callBack Function
set(h1,'Callback','Button_1_callBack_Function');
%Setting the string on the button
set(h1,'String','Push.B_1');
%Setting Tool Tip String
set(h1,'TooltipString','Push Me To Call The CallBack Function!');
%Setting The Text Color on the button (the foreground color)

set(h1,'ForegroundColor',[0.0,.0,0.0]);
%setting the background color
set(h1,'BackgroundColor',[0.0,1.0,0.0]);

Building a Graphical User Interface with MATLAB

Edit - Box call back function.


EditBox_H=GCBO;
EditBoxString=get (EditBox_H,'String')

I - 40