; Matlab_tutorial
Learning Center
Plans & pricing Sign in
Sign Out



  • pg 1
									EE 3317 Matlab Tutorial       - Fall 09

1) What is Matlab?

MATLAB is a technical computing environment for high-performance numeric
computation and visualization. MATLAB integrates numerical analysis, matrix
computation, signal processing, and graphics in an easy-to-use environment where
problems and solutions are expressed just as they are written mathematically - without
traditional programming.

MATLAB is an interactive system whose basic data element is a matrix that does not
require dimensioning. This allows you to solve many numerical problems in a fraction
of the time it would take to write a program in a language such as Fortran, Basic, or

MATLAB also features a family of application-specific solutions called toolboxes.
Toolboxes are collections of MATLAB functions (M-files) that extend the MATLAB
environment in order to solve particular classes of problems.

2) Getting help in Matlab

MATLAB has an extensive help system built into it, containing detailed
documentation and help information on all of the commands and functions of
MATLAB. This help system is extremely useful for beginners to MATLAB, but even
after becoming an expert at MATLAB, you will still use the help system to learn
about other MATLAB functionalities that you have not yet used before.
There are three main functions that you can use to obtain help on a given function:
    help
    helpwin
    doc
   or using the MATLAB help browser.

3) File Management Commands

 cd : show the present directory
 dir : show the files in the present directory
 cd dir: changes the directory.
type filename: list all the contents of filename
which filename: displays the path of file name. This can be very useful to know if a
file is part of standard Matlab package
what: list the .m files that are in directory
who : list all variables in the workspace
whos : list all variables in the workspace along with size in bytes, array dimensions,
and object type
clear : removes all variables from the workspace, it is very helpful to type it at the
beginning of each M-file in order to clear the space every time you rerun the same
clear x: removes just the variable x.
clc: clean the command window

4) Load and save data and results

To load data, you need the data to be an ascii file, that does not contain any label or
date, just columns of number. It is very easy if you have your data in an excel file,
then cancel the column of the date and the first rows that usually just contains the
name of the variables, and save them in txt format. Then to load the matrix of data,
load specify path where data file is\ filename.txt;
Example : load c:\Stefania\nonpar\return.txt;
To save data or matrix of results, type:
save path that specify where you want to save the file\ new filename matrix_name
Example: save d:\Das\beta betaf -ascii;

5) Arithmetic operaters

Operators are the usual:
* : multiplication;
/ : division;
+: addition;
-: subtraction;
^: exponentiation
‘: transposition for non complex matrices

Remember when you multiply two matrices A and B they must be dimensionally
correct. It is often useful to perform operation element by element. To do this, we add
a period before the operator:
.* : multiply element-by-element
./ : divide element-by-element
.^: Raise elements to powers
Example to square each element of a matrix A, we have to use A.^2.

6) Matlab Punctuation

% It denotes a comment line. To write comments inside the program that can be
useful (to yourself or to other people who read the program) to keep track of what you
are doing use: % at the beginning of the comment and % at the end of it. This allows
the comment to be ignored and not to interfere with the commands of the program.
, A comma tells Matlab to display the results. A blank space works in the same way.
It also concatenates array element along a row. Example type:
>> a=[1 2 3, 4 5 6]

      1      2       3          4      5      6

the numbers have been concatenated in a unique row.

; A semi-colon suppresses printing the contents of the variable to the screen, for this
reason it is very important to terminate each command with semi-colon. It also
concatenates array elements along a column. Example type:
>> A=[1 2 3; 4 5 6]

      1      2       3
      4      5       6

… Three periods denotes the continuation of a statement. If a command is too long
and you want to continue typing in the line below, then use it.
     A = [ ...
     16.0      3.0      2.0     13.0
      5.0     10.0       11.0        8.0
      9.0      6.0        7.0       12.0
      4.0     15.0       14.0        1.0 ];
: The colon specifies a range of numbers. You will use it accessing elements of
arrays. Example type:
>>b=A(:,1) % using the A matrix defined above, it means that b will take ALL
THE ROWS in A and the first column, i.e. b accesses the first column of matrix A%.
Instead if we type:
>>c=A(1,:) % we select the first row and of course all columns%
16 3 2 13

. The period before an operator as we said before implies that we perform the
corresponding operation on each element of the array.

7) Creating and Manipulating Matrices
    You can create matrices explicitly putting each row on a separate line
   A=[ 13 3 10
   2     3*pi      9]
   or putting a semicolon between rows:

   A=[13 3 10; 2 3*pi     9]

       You can load matrices from a text file
       Special Matrices

zeros: This builds arrays containing all 0’s. Example type:
>> A=zeros(2,2)

       0      0
       0      0

ones: This builds arrays containing all 1’s. Example type:
>> A=ones(2,2)

         1      1
         1      1

eye: This creates an identity matrix. Example type:
>> A=eye(2,2)

     1       0
     0       1
magic: M = magic(n) returns an n-by-n matrix constructed from the integers 1 through
n^2 with equal row and column sums. The order n must be a scalar greater than or
equal to 3.
transpose: Return the nonconjugate transpose of an object

ctranspose: Return the complex conjugate transpose of an object

8) Graph

XY Plots

         Simple xy-plot, all the points are connected by lines: plot(xdata,ydata);
         Plot with control over the drawing parameters: plot(xdata,ydata,S) where S is
          a string (enclosed in single quotes) specifying the drawing format. Options
                               y       yellow          .       point
                               m        magenta          o       circle
                               c       cyan             x       x-mark
                               r      red              +       plus
                               g       green           -       solid
                              b       blue             *      star
                              w       white             :     dotted
                              k       black            -.    dashdot
                                                        --    dashed
       For example, PLOT(X,Y,'c+') plots a cyan plus at each data point.

      To plot more than one set of data on the same set of axes, you can either do:
       plot(xdata1,ydata1,S1,xdata12,ydata2,S2,...) where the S parameters are still


       hold on (this prevents the next plot command from erasing the figure window)

Titles and labels

      Titles: title('title_text')
      Labels: xlabel('label_text'); ylabel('label_text')
      Text at location x,y: text(x,y,'string')
      Text placed with mouse: gtext('string')

9) M-files

Matlab Programs are saved in M-files. These are text files that contain Matlab
commands, and they are saved with .m extension. Any text editor can be used to
create them, but that one that comes with Matlab is recommended. When the .m files
are executed, the commands are implemented as if you typed them directly!!!! An .m
file can contain and call other .m files. For example in one .m file you have
programmed the objective function that you want to minimize or maximize, and then
in another .m file where you have loaded the data and write down the command to
implement the optimization you can call the .m file that contains the objective
function. To execute the .m simply type the name of the file at the command line. It is
important to note that .m that contain function have a special syntax for the first line.
function [out1,…outN] = func_name (inp1,….,inpN)
function f = kb4_gum(param)
in this case I have only one output f and only one imput param.
The function corresponding to the above syntax should be saved in a file called
func_name.m. Hence in the above example, the file containing that first line has to
be saved as kb4_gum.m.
10) Output form
While all computations in MATLAB are performed in double precision, the format of
the displayed output can be controlled by the following command.

format short          fixed point with 4 decimal places(the default)
format long            fixed point with 14 decimal places
format short e         scientific notation with 4 decimal places
format long e         scientific notation with 15 decimal places
format hex              hexadecimal format
format bank            dollars and cents
format rat             approximate integer ratio
format +               +,-,and blank

11) Workspace

workspace displays the Workspace browser, a graphical user interface that allows you
to view and manage the contents of the workspace in MATLAB. It provides a
graphical representation of the whos display, and allows you to perform the
equivalent of the clear, load, open, and save functions.

The Workspace browser also displays and automatically updates statistical
calculations for each variable, which you can choose to show or hide.
12) Useful function in solving #HW 1


Some useful link to check for Matlab:

1. http://www.engin.umich.edu/group/ctm/basic/basic.html
2. http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
   (official site for Math Works)
3. http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html
4. http://web.mit.edu/vchudnov/www/matlab.html

Or books in UTA library:

5. Signals and systems [electronic resource] : with MATLAB applications / Steven T.
6. Getting started with MATLAB : a quick introduction for scientists and engineers /
   Rudra Pratap.
7. Elementary mathematical and computational tools for electrical and computer
   engineers using MATLAB [electronic resource] / Jamal T. Manassah.

To top