M. Nelson Spring 2004

New Matlab Commands

(to get more info, type 'help <cmd>' at the matlab prompt)

arrays |
a(i)
is the ith element of a vector; a(i,j) is element (i,j) of a 2-D matrix Matlab matrices can have more than 2 dimensions. Matlab indexing is 1-based, not 0-based like some other languages, so the first element is a(1), not a(0). |
||||||||||||||||||

axis | In HW0 we discovered that axis([xmin xmax ymin ymax]) sets scaling for the x- and y-axes. Here we show some addition things the 'axis' command can do: axis equal sets the aspect ratio so that equal tick mark increments on the x-,y- and z-axis are equal in size. axis square makes the current axis box square in size. axis normal restores the current axis box to full size and removes any restrictions on the scaling of the units. This undoes the effects of axis square and axis equal. axis off turns off all axis labeling, tick marks and background. axis on turns axis labeling, tick marks and background back on. |
||||||||||||||||||

fix |
fix(x)
rounds x to the
nearest integer towards zero. See also floor, round, ceil |
||||||||||||||||||

for/end |
repeat statements a specific
number of times for i = 1:N a(i) = sqrt(i); end |
||||||||||||||||||

if/else/end |
The general form of the if statement is if expression statements elseif expression statements else statements end The statements are executed if the real part of the expression has all non-zero elements. The else and elseif parts are optional. The expressions oftent contain relational operators, such as <, <=, >, >=, ==, and ~=. See also relop. |
||||||||||||||||||

length |
length(x)
returns the length of vector x.
It is equivalent to max(size(x))
for non-empty arrays and 0 for empty ones. |
||||||||||||||||||

max |
For vectors, max(x) is the largest element
in x For matrices, max(x) is a row vector containing the maximum element from each column of x. |
||||||||||||||||||

min |
For vectors, min(x) is the smallest element
in x For matrices, min(x) is a row vector containing the minimum element from each column of x. |
||||||||||||||||||

num2str |
txt
= num2str(x) converts the variable x into a string representation txt with about 4 digits and an exponent if required. This is useful for labeling plots with the title, xlabel, ylabel and text commands. |
||||||||||||||||||

rand |
Uniformly distributed random
numbers on the interval (0.0,1.0). rand is a single random number rand(m,1) is an m-by-1 column vector with m random entries rand(m,n) is an m-by-n matrix with m x n random entries. rand('state',0) resets the generator to its initial state. |
||||||||||||||||||

relop |
relational operators
Example: if ( (v(i) > thresh) & (t >= tstart) & (t < tstop) ) statements end |
||||||||||||||||||

size |
[m,
n] = size(x) for vector/matrix x, returns the number of rows m and columns n as separate output variables. d = size(x) returns the size as a two-element vector d = [m, m] |
||||||||||||||||||

text |
text(x,
y, 'string') adds the text in the quotes to location (x,y) on
the current axes, where (x,y) is in units from the current plot. |
||||||||||||||||||

who |
who
lists the variables in the current workspace |
||||||||||||||||||

whos |
lists more information about
each variable, including size |

Example Script

(you can copy-and-paste this into the matlab command window or save it as an m-file)

clear; figure(1); clf; % number of points NPT = 6; % generate random (x,y) coordinates rand('state',0); x = rand(NPT,1); y = rand(NPT,1); % plot the points plot(x, y,'ro-'); hold on; % add labels for ipt= 1:NPT myString = ['Point ' num2str(ipt)]; text(x(ipt)+.02 , y(ipt)+.02, myString); end % set the axis limits axis([0 1 0 1]); grid on; axis equal; |