MATLAB BASICS Starting Matlab < PC: Desktop icon or Start menu item < UNIX: Enter matlab at operating system prompt < Others: Might need to execute from a menu somewhere Entering Matlab commands < Matlab command prompt is >> < Type commands or variable names and expressions, then RETURN to execute < Ending a command line with ; suppresses echoing of result, else echo occurs < Use more on and more off to toggle screen-by-screen display < Use help for general help, help command for help on specific command or function < Up arrow scrolls back through previously entered commands (& Down forward) < Highlighting and using Cut/Ctrl-x, Copy/Ctrl-c, Paste/Ctrl-v works Stopping Matlab < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c
MATLAB VARIABLES AND DATA Matlab is a calculator (so +, -, *, /, ^, sin, tan, log, exp, sqrt mean what they usually do) Matlab easily handles arrays (vectors, matrices, multidimensional arrays): < x=1 is a scalar < x=[1 2 3 4] is a row vector < x=[1 2 3 4]' or x=[1; 2; 3; 4] or x=[1 <RET> 2 <RET> 3<RET> 4] is a column vector < x=[1 2 3 4; 5 6 7 8] or x=[1 2 3 4 <RET> 5 6 7 8] is a (2x4) matrix < Variable names can be up to 31 characters long and are case-sensitive < Type variable <RET> to see its value < Type who to list all current variables < Type whos to list all current variables and their types and sizes < size(y) gives dimensions of variable y < Clear a variable name with clear variable - NOTE: clear clears EVERYTHING < Last answer is variable ans < Use format to alter display format (fixed point, scientific notation, etc.) Matlab does complex variables, too (e.g., x=6+5*i if i is not overwritten) Matlab lets you save the current variables and load previously saved data: < save file saves everything to filename file.mat (in current directory) < save file x y saves only x and y to filename file.mat (in current directory) < load file loads all variables from filename file.mat in directory path and overwrites < load dir/file loads all variables from filename file.mat (in directory dir) and overwrites < Omitting filename defaults to matlab.mat in current directory < Options exist for loading and saving ASCII text data - see help on load and save
MATLAB DIRECTORIES Matlab starts in an application-dependent default directory < pwd to see current directory < dir or ls for current directory contents < cd to change current directory < what to see Matlab files in current directory <! to execute a shell command outside of Matlab Matlab starts with an application-dependent default directory path (to look for things) < path to see current path (searches top to bottom) < path(path, dir ) to append directory dir to end of path < See help on path for other options
MATLAB AS A PROGRAMMING LANGUAGE The colon : specifies ranges of values: < x=[1:10] is x=[1 2 3 4 5 6 7 8 9 10] < x=[0:0.1:1.0] is x=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0] < Negative or variable increments and limits are OK: x=[ylow:(yhigh-ylow)/20:yhigh] < Useful for all or part of rows, columns, other dimensions of arrays: < a(3,:) is third row of a < b(:,4:5) is fourth and fifth columns of b < c(1,3,2:6) is second through sixth elements along third dimension of array c(1,3) Matlab recognizes relational operators <, >, <=, >=, ==, ~=: < For a row array x, x>=0 produces a row array with 1 where true and 0 where false < Other commands also produce 1/0 true/false logical output (e.g. isreal, isprime) Matlab does if/(then)/else, for, and while: < Must ALWAYS terminate with end < if x<=pi/2 y=sin(x) else y=-sin(x) end < y=0; for k=[0:.1:pi/2] y=[y; sin(k)]; end You can save Matlab scripts as.m (text) files: < Form a text file (using any editor, including Matlab editor) containing: y=0; for k=[0:.1:pi/2] y=[y; sin(k)]; end < Save it as file myscript.m in the current directory or in a directory on the path < Type myscript to execute it < NOTE: Re-save any changes made in an editor before re-executing < You can also define Matlab functions this way (manipulate internal variables but only the designated output variables go back to the Matlab workspace) < Within scripts (or functions), you can display things to the screen ( disp ), execute other Matlab functions or scripts, ask for user input ( input ), even pause for a specific length of time or until a keyboard key is pressed
MATLAB FOR PLOTS The key command here is plot : < plot(x,y) plots y (vertical axis) vs. x (horizontal) where x and y must be vectors that are exactly the same size with auto-scaled axis ranges < See help for plot for many other options (semilog plots, different symbols and line colors, 3-dim feather plots, etc.) < Plot appears in a separate window, labelled Figure No. X where X starts as 1 < Some operations to enhance plots: grid adds a grid, xlabel and ylabel add labels to axes, title adds title < figure in command window switches to next plot window (2 if X was 1 above) < figure(3) switches to window containing Figure No. 3 < hold on allows a new plot on same axes as previous plot ( hold off turns it off) < axis([1 6-3 4]) fixes the axis ranges to 1 to 6 along x and -3 to 4 along y, axis with no specified ranges turns auto-scaling of axes back on Once a plot is created: < You can copy it and then paste it into a word processor < You can print it < plot(x,y) plots y (vertical axis) vs. x (horizontal) where x and y must be vectors that are exactly the same size with auto-scaled axis ranges < See help for plot for many other options (semilog plots, different symbols and line colors, 3-dim feather plots, etc.) < Plot appears in a separate window, labelled Figure No. X where X starts as 1 < Some operations to enhance plots: grid adds a grid, xlabel and ylabel add labels to axes, title adds title < figure in command window switches to next plot window (2 if X was 1 above) < figure(3) switches to window containing Figure No. 3 < hold on allows a new plot on same axes as previous plot ( hold off turns it off) < axis([1 6-3 4]) fixes the axis ranges to 1 to 6 along x and -3 to 4 along y, axis with no specified ranges turns auto-scaling of axes back on