Basic Matlab commands
A Few Comments On These Notes
- They are intended to be self contained hands on tutorial, with enough explination so the reader can learn the basics of Matlab simply by reading these notes and performing the commands shown
- The words in bold are intended to be typed into Matlab at the command prompt just as they are shown as examples
- At the end of these notes, you will find a text file generated when these commands were entered into a Matlab session. This attachment is provided so you can verify your results from this tutorial.
Starting Matlab
- Click on Start Button
- Highlight Programs, select Matlab
Getting Help
Command Line
- Type "help " then name of command, function or toolbox you want help with
- If you don't know the name of the command, function, or toolbox you want help with, you can simply type help then a list of the toolboxes will be displayed
Help Window
- Click on the question mark on the tool bar, or
- Select "Help" from the menu, then click on "Help Window" from the pull down menu
Help Desk
- Type "helpdesk" at command prompt (>>), or Helpdesk
- Select "Help" from the menu, then click on "Help Desk (HTML)" from the pull down menu, or
- From the Help Window, click on the button titled "Go to Help Desk"
Variables
- Variable names must start with a letter, but can be followed by any number of letters, digits, or underscores.
- Variable names are case sensitive, e.g. "R" is not the same variable as "r"
- Examples
- R=6
- Circ=2*3.1416*R
- Matlab has some built in variables, i.e. pi and i (see Help Desk/Getting Started/Expressions/Functions)
- Note: These built in variables are not reserved, and therefore they can be overwritten!
- Examples
- Circumf=2*pi*R
- pi=4
- Circumf2=2*pi*R
- imag1=3+6i
- imag2=7+9j
- imag3=imag1+imag2
Matrices (arrays)
At this point I'll introduce on of several useful functions, one of which is whos.
whos lists all currently assigned variables, their size, bytes used, and the class. You'll notice that at this point all the variables have a size of 1x1. Matlab treats all variables as matrices (arrays), matrices (arrays) of size 1x1 are scalars
Entering Arrays
There are several ways of entering Arrays
- Manually
- A=[1 2 3;4 5 6;7 8 9]
- Using the colon function
- The colon function creates a sequence of numbers
- Format of colon operator is as follows: beginning number : increment : ending number
- E=1:10 or E=[1:10]
- F=2:2:10
- One of Matlab's Array generating functions
- B=zeros(3,3) creates a 3x3 array full of zeros
- C=ones(3,3) creates a 3x3 array full of ones
- D=round(10*rand(3,3)) creates a 3x3 array of random integers between 0 and 10
Matrix Manipulation Once you have an Array, you can manipulate it quite easily Say, you wanted just the 1st two rows of Array "A" G=A([1 2],:) Or just the first two columns but in reverse order H=A(:,[2 1]) Or just the upper 2x2 of Array "A" I=A(1:2,[1 2]) or I=A([1 2],[1 2]) or I=(1:2,1:2) What if you only wanted to know the value of the 2nd number down in the 1st column of "A" J=A(2,1) You can also transpose a matrix simply e=E' O=A'
Element by Element Operators Any Matrix (Array) operated on by a scalar is done element by element K=A+2 L=A*2 To perform element by element multiplication or division between two matrices the ".*","./", or ".^"operators must be used M=A.*D N=A./D P=A.^2 Note: the division operator is directional (see: Help Desk/Matlab Functions by Subject/Operators and Special Characters/.)
Some More Useful Functions At this point lets introduce a few other useful functions; "clear", and "clc" "Clear" clears the workspace or particular variables from memory the format for "clear" is as follows clear variable_name_1 variable_name_2 ... or without any variable names it clears all variables whos clear A B whos clear whos "clc" clears the command window At this point you can scroll up in the command window and review the history of this session clc At this point you can no longer scroll up in the command window
Matrix Operations Multiplication First lets enter some Matrices A=[1 2 0;2 5 -1;4 10 -1] D=[3 8;7 2;6 4;12 5] Lets take the transpose of each of them B=A' E=D' Lets Multiply the original matrix by it's transpose C=A*B F=D*E As you notice this is true matrix multiplication, and must therefore you must comply with those rules, for example if you try to multiply "A" by "D" you will get an error G=A*D Divisions and Powers can be performed also Inversion Matlab has a special function to invert a matrix B=inv(A) To prove this is truly the inverse, lets multiply "A" by "B" C=A*B Eigenvalues and Eigenvectors Matlab also has a special function to find the Eigenvalues and Eigenvectors of a Matrix To get the Eigenvalues: C=eig(A) This produces a vector of the eigenvalues To get the eigenvalues "D" and the eigenvectors "V" (such that A*V=V*D) [V,D]=eig(A)
Plotting The plot command for 2-dimensional plots First lets clear the memory clear Now lets set-up a couple of vectors X=0:pi/12:4*pi; Y1=3*cos(X); Note: The semi-colon at the end of the above statements suppresses the display of the arrays being created Now let's plot "Y" versus "X" plot(X,Y1) You can also change the line style, color, and include symbols, you'll now generate a plot with a dashed red line, and circular symbols (see "help plot" or Help Desk and searching for "linespec" for more info on types of lines and symbols) plot(X,Y1,'--ro') You can also add titles to the axis and the figure as follows ylabel('amplitude') xlabel('angle') title('amplitude vs. angle') You can also change the scale on the axis The format for this command is : Axis([xmin xmax ymin ymax]) axis([0 4*pi -6 6]) You can also plot more than one variable on one figure So lets create another variable, and plot it to the existing figure Y2=5*sin(X); hold this holds the current plot so additional variables can be added plot(X,Y2,'-bx') hold Or you could have done it as follows plot(X,Y1,'--ro',X,Y2,'-bx') You can clear the figure by typing "clf" clf
Other Useful Commands I'll let you read about these on your own in the on-line help save Saves variables so they can be recalled later diary creates a diary of the current session beginning where the command is invoked addpath Adds directories to the default search paths in Matlab
Related Articles:
What software is on lab computers
Where are the college computer labs located, and how can I reserve one for a class?
KEC 1130 Computer Lab
What software is on the windows systems in Radiation Center A124
Article Information:
Date Created: Sep 15, 2003
Last Modified: Mon, Jun 25, 2007 1:22 PM
Views: 140854