Sie sind auf Seite 1von 32

Programming in Matlab

Programming in Matlab

Programming in Matlab

Flow control - selection


function
functioncal(x,y,o)
cal(x,y,o)
The switch construction

switch(o)
switch(o)

Switch<expression>

case
case'+'
'+'

case <condition>,

x+y
x+y

<statement>
otherwise< condition

>,

< statement>

case
case'-''-'
x-y
x-y
case
case'*'
'*'

end

x*y
x*y
case
case'/''/'
x/y
x/y
end

Programming in Matlab

Plotting in Matlab

Programming in Matlab

TWO-DIMENSIONAL plot() COMMAND


The basic 2-D plot command is:

plot(x,y)
where x is a vector (one dimensional array), and y is a vector.
Both vectors must have the same number of elements.
The plot command creates a single curve with the
the abscissa (horizontal axis) and the
(vertical axis).

x values on

y values on the ordinate

The curve is made from segments of lines that connect the


points that are defined by the
elements in the two vectors.

and

coordinates of the

Programming in Matlab
PLOT OF GIVEN DATA

Given data:
x

7.5

10

6.5

5.5

A plot can be created by the commands shown below. This can be


done in the Command Window, or by writing and then running a
script file.
>> x=[1 2 3 5 7 7.5 8 10];
>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)
Once the plot command is executed, the Figure Window opens with
the following plot.

Programming in Matlab
PLOT OF GIVEN DATA

Programming in Matlab
LINE SPECIFIERS IN THE plot() COMMAND
Line specifiers can be added in the plot command to:
Specify the style of the line.
Specify the color of the line.
Specify the type of the markers (if markers are desired).

plot(x,y,line specifiers)

Programming in Matlab

LINE SPECIFIERS IN THE plot() COMMAND

plot(x,y,line specifiers)

Line
Style
Solid
dotted
dashed
dash-dot

Specifier
:
--.

Line
Color
red
green
blue
Cyan
magenta
yellow
black

Specifier
r
g
b
c
m
y
k

Marker Specifier
Type
plus sign
circle
asterisk
point
square
diamond

+
o
*
.
s
d

Programming in Matlab
LINE SPECIFIERS IN THE plot() COMMAND

10

The specifiers are typed inside the plot() command as strings.


Within the string the specifiers can be typed in any order.
The specifiers are optional. This means that none, one, two, or
all the three can be included in a command.
EXAMPLES:
plot(x,y)

A solid blue line connects the points with no markers.

plot(x,y,r)

A solid red line connects the points with no markers.

plot(x,y,--y)

A yellow dashed line connects the points.

plot(x,y,*)

The points are marked with * (no line between the


points.)

plot(x,y,g:d)

A green dotted line connects the points which are


marked with diamond markers.

Programming in Matlab
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot() COMMAND
Year

1988

1989

1990

1991

1992

1993

1994

Sales (M)

127

130

136

145

158

178

211

>> year = [1988:1:1994];


>> sales = [127, 130, 136, 145, 158, 178, 211];
>> plot(year,sales,'--r*')

Line Specifiers:
dashed red line and
asterisk markers.

11

Programming in Matlab
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot() COMMAND

12

Dashed red line and


asterisk markers.

Programming in Matlab

Formatting the plots

13

Programming in Matlab
Plot title EXAMPLE OF A FORMATTED 2-D PLOT
Light Intensity as a Function of Distance

1200

14

Legend

Theory
Experiment

y axis
label

1000

Text

Tick-mark

INTENSITY (lux)

800
Comparison between theory and experiment.
600

400

Data symbol

200

10

12

x axis
label

14

16
18
DISTANCE (cm)

20

22

24

Tick-mark label

Programming in Matlab
FORMATTING PLOTS
A plot can be formatted to have a required appearance.
With formatting you can:
Add title to the plot.
Add labels to axes.
Change range of the axes.
Add legend.
Add text blocks.
Add grid.

15

Programming in Matlab
FORMATTING COMMANDS
title(string)
Adds the string as a title at the top of the plot.

xlabel(string)
Adds the string as a label to the x-axis.

ylabel(string)
Adds the string as a label to the y-axis.

axis([xmin xmax ymin ymax])


Sets the minimum and maximum limits of the x- and y-axes.

16

Programming in Matlab
FORMATTING COMMANDS
legend(string1,string2,string3)
Creates a legend using the strings to label various curves
(when several curves are in one plot). The location of the
legend is specified by the mouse.

text(x,y,string)
Places the string (text) on the plot at coordinate x,y relative to
the plot axes.

17

Programming in Matlab
EXAMPLE OF A FORMATTED PLOT

Syntax:

colo
r

line

marker

plot(x1,
plot(x1, y1,
y1, 'clm1',
'clm1', x2,
x2, y2,
y2, 'clm2',
'clm2', ...)
...)

Example:
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
plot(x,y,x,z)
plot(x,y,x,z)
title('Sample
title('Sample Plot','fontsize',14);
Plot','fontsize',14);
xlabel('X
xlabel('X values','fontsize',14);
values','fontsize',14);
ylabel('Y
ylabel('Y values','fontsize',14);
values','fontsize',14);
legend('Y
data','Z
legend('Y data','Z data')
data')
grid
grid on
on

18

Programming in Matlab

19

Sample Plot
Title

Ylabel

Grid

Legend
Xlabel

Programming in Matlab

20

Displaying Multiple Plots


Two typical ways to display multiple curves in
MATLAB (other combinations are possible)
One figure contains one plot that contains
multiple curves
Requires the use of the command hold (see
MATLAB help)
One figure contains multiple plots, each plot
containing one curve
Requires the use of the command subplot

Programming in Matlab
One Plot Contains Multiple Curves

Example:
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
plot(x,y,x,z)
plot(x,y,x,z)
grid
grid on
on
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
plot(x,y,b)
plot(x,y,b)
hold
hold on
on
plot(x,z,g)
plot(x,z,g)
hold
hold off
off
grid
grid on
on

21

Programming in Matlab

Sample Plot

22

Programming in Matlab

23

Subplots
subplot divides the current figure into rectangular panes that
are numbered rowwise.
subplot(rows,cols,index)
Syntax: subplot(rows,cols,index)
subplot(2,2,1);
subplot(2,2,1);

subplot(2,2,2)
subplot(2,2,2)
...
...
subplot(2,2,3)
subplot(2,2,3)
...
...
subplot(2,2,4)
subplot(2,2,4)
...
...

Programming in Matlab
One Figure Contains Multiple Plots

Example:
x=[0:0.1:2*pi];
x=[0:0.1:2*pi];
y=sin(x);
y=sin(x);
z=cos(x);
z=cos(x);
subplot(1,2,1);
subplot(1,2,1);
plot(x,y)
plot(x,y)
subplot(1,2,2)
subplot(1,2,2)
plot(x,z)
plot(x,z)
grid
grid on
on

24

Programming in Matlab

25

Programming in Matlab

26

Programming in Matlab

27

Programming in Matlab

28

Programming in Matlab

29

Programming in Matlab

30

Programming in Matlab

31

Programming in Matlab

THE END

32

Das könnte Ihnen auch gefallen