PART 4: graphics

MATLAB techniques for modeling and simulation

MCB 419: Brain, Behavior and Information Processing

M. Nelson, Univ. of Illinios, Urbana-Champaign, Jan 2005

Contents

MATLAB graphics

MATLAB's graphics and visualization abilities are impressive. In this tutorial, we'll start with the basics of plotting lines and points and drawing circles and rectangles.

figure

the figure command creates or switches between figure windows by itself figure creates a new figure window, and returns its handle. figure(H) makes H the current figure, forces it to become visible, and raises it above all other figures on the screen. If figure H does not exist, and H is an integer, a new figure created with handle H.

clear
figure(1);

plot

the plot command adds points and lines plot(x,y) plots vector y versus vector x

x = 0.0: 0.1: 2.0;
y = sqrt(x);
plot(x,y);

plot(y) plots y versus it's index.

plot(y)

line types, plot symbols and colors

various plot styles may be obtained with plot(x,y,str) where str is a character string made from one element from any or all the following 3 categories: color: r,g,b,c,m,y,k (red green blue cyan magenta yellow black) markers: . o x + * s d (point circle x-mark plus star square diamond) line-styles: - (solid), : (dotted), -.(dashdot), -- (dashed)

% type 'help plot' or 'doc plot' for more info

plot(x, 0.2*x, 'r.-');
hold on;
plot(x, 0.4*x, 'bo');
plot(x, 0.6*x, 'g--');

hold

hold on holds the current plot and all axis properties so that subsequent graphing commands add to the existing graph. hold off| returns to the default mode whereby PLOT commands erase the previous plots and reset all axis properties before drawing new plots.

hold off

axis

axis controls axis scaling and appearance axis([xmin xmax ymin ymax]) sets scaling for the current plot.

plot([0 1 1 0],[0 0 1 1],'r-');
axis([-2 2 -2 2]);

axis equal sets the aspect ratio so that equal tick mark increments on the x-,y- and z-axis are equal in size.

axis equal

rectangle

rectangle('Position', [x y w h]) adds a rectangle with lower left corner at (x, y) and specified width (w) and height (h).

rectangle('Position',[-1 -1 4 3]);
axis([-5 5 -5 5]);

circle

there is no 'circle' command in MATLAB. Instead use the 'rectangle' command, but specify 'Curvature', [1 1]:

hMyCircle = rectangle('position',[-2 -2 2 2],'curvature',[1,1]);

graphics handles

graphics handles are optionally returned from all graphics creation calls. In the example above, hMyCircle is a graphics handle to the circle object that we just created. We can use the handle to modify the circle after it's already been created, including changing its color, size, position, etc.

set(hMyCircle,'Position',[2 -2 2 2],...
    'EdgeColor','r',...
    'FaceColor','y',...
    'LineWidth',4);

get

use get(H) to get a listing of all the properties of the graphics object associated with handle H

get(hMyCircle)
	Curvature = [1 1]
	EraseMode = normal
	FaceColor = [1 1 0]
	EdgeColor = [1 0 0]
	LineStyle = -
	LineWidth = [4]
	Position = [2 -2 2 2]

	BeingDeleted = off
	ButtonDownFcn = 
	Children = []
	Clipping = on
	CreateFcn = 
	DeleteFcn = 
	BusyAction = queue
	HandleVisibility = on
	HitTest = on
	Interruptible = on
	Parent = [154.003]
	Selected = off
	SelectionHighlight = on
	Tag = 
	Type = rectangle
	UIContextMenu = []
	UserData = []
	Visible = on

use get(H,'property') to access an individual property

get(hMyCircle,'position')
ans =

     2    -2     2     2

use set(H,'property',value) to set an individual property

set(hMyCircle,'LineStyle','--');

use set(H) displays all property names and their possible values

set(hMyCircle)
	Curvature
	EraseMode: [ {normal} | background | xor | none ]
	FaceColor
	EdgeColor
	LineStyle: [ {-} | -- | : | -. | none ]
	LineWidth
	Position

	ButtonDownFcn: string -or- function handle -or- cell array
	Children
	Clipping: [ {on} | off ]
	CreateFcn: string -or- function handle -or- cell array
	DeleteFcn: string -or- function handle -or- cell array
	BusyAction: [ {queue} | cancel ]
	HandleVisibility: [ {on} | callback | off ]
	HitTest: [ {on} | off ]
	Interruptible: [ {on} | off ]
	Parent
	Selected: [ on | off ]
	SelectionHighlight: [ {on} | off ]
	Tag
	UIContextMenu
	UserData
	Visible: [ {on} | off ]

clf

clf clears the current figure

clf