| Introduction to MATLAB | |||||||||||||||||||||||||||
|
Other online documents available:
Please note that this page was written for Matlab version 5.0a, and this site is now running Matlab 6.5. This update was not a major shift in the way Matlab is run, but some of the Matlab and Simulink features, as well as some of the toolboxes, have been changed. If you note a discrepancy, please report it to the class instructor.   | |||||||||||||||||||||||||||
|   | Page Index | ||||||||||||||||||||||||||
|   |
  | ||||||||||||||||||||||||||
|   | 1. What is MATLAB? | ||||||||||||||||||||||||||
|   |
MATLAB is a high-performance, interactive software package for scientific and engineering numeric computation. MATLAB integrates numerical analysis, matrix computation, signal processing, and graphics in an easy-to-use environment without traditional programming. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to the matrix software developed by the LINPACK and EISPACK projects. MATLAB is an interactive system whose basic data element is a matrix that does not require dimensioning. Furthermore, problem solutions are expressed in MATLAB almost exactly as they are written mathematically. MATLAB has evolved over the years with input from many users. MATLAB TOOLBOXES are specialized collections of MATLAB files designed for solving particular classes of functions. These packages are currently available:
  | ||||||||||||||||||||||||||
|   | 2. Accessing MATLAB at ER4 | ||||||||||||||||||||||||||
|   |
Log onto a workstation. Use the mouse button to view the Root Menu. Drag the cursor to "Utilities" then to "Matlab." Release the mouse button. You are now running the MATLAB interactive software package. The prompt is a double "greater than" sign. You may use emacs line editor commands and the arrow keys while typing in MATLAB. MATLAB can also be started from any terminal window (dtterm), with display capabilities, using the command matlab.   | ||||||||||||||||||||||||||
|   | 3. An Introductory Demonstration | ||||||||||||||||||||||||||
|   |
Execute the following command to view a quick introduction to MATLAB. >> demo Select   | ||||||||||||||||||||||||||
|   | 4. Making and Printing Graphs | ||||||||||||||||||||||||||
|   |
  | ||||||||||||||||||||||||||
|   |   | i. Example Signal | |||||||||||||||||||||||||
|   |   |
MATLAB represents objects as vectors and matrices. Therefore, a
signal is represented by a vector of samples. Generate 500 samples of a sine wave of frequency 60 Hz and sampled at 8 kHz.
>> n = [0:499]/8000;The time indices and signal amplitudes are stored in th row vectors n and x, respectively. note that MATLAB returns the result of command unless output is suppressed by ending the command with a semicolon.
| |||||||||||||||||||||||||
|   |   | ii. Plot Commands | |||||||||||||||||||||||||
|   |   |
Creating graphs in MATLAB is easy.
>> plot(x)You can specify two vectors of equal length in the plot command to specify both the horizontal and vertical axes.
>> plot(n,x)Note how the horizontal axes is now in units of seconds, rather than sample number. Let's title the graph, label the axes, and place a grid on the plot.
>> plot(n,x)By specifying elements of the vectors, you may plot a selected portion of the signal.
>> plot(n(1:50),x(1:50))SUBPLOTS We can place several graphs on a single page using the subplot command. the arguments in subplot specify the number of rows and columns, then the position of the plot.
>> subplot(2,1,1),plot(x,n)AND MUCH MORE! The MATLAB graphics tools provide options for line types, overlaid curves, data point symbols, line colors, etc. Use the help command to explore these commands:
For a colorful demo for MATLAB graphics, type
>> demonote that the GUI (graphical user interface) tools to build the demo are part of MATLAB, and can be used to make menu-driven, point-and-click program interfaces for course work, teaching, and research projects.   | |||||||||||||||||||||||||
|   |   | iii. Printing | |||||||||||||||||||||||||
|   |   |
To print your figure we will first create a file called "temp.ps"
containing the plot, then view the file on the screen, then send the
file to the printer. In this fashion, you can save files of figures
you generate using MATLAB, and can view those files on your
workstation screen, and can incorporate your MATLAB plots into
documents generated using LaTeX, Word, Claris, etc.
>> print temp -f1The "-f1" option selects the graph window labeled "Figure No. 1." In your workstation window, type
dtterm> ghostview temp.psIf a printout is desired, then the figure or the .ps file can be sent to the printer from MATLAB or the workstation window (dtterm) To clear the figure in MATLAB and to return to plotting only one graph in a figure, type clg for "clear graph."   | |||||||||||||||||||||||||
|   |   | iv. Importing Data for Graphing | |||||||||||||||||||||||||
|   |   |
Data Generated by other programs can be ported into MATLAB for easy
graphics or further computation. See Appendix
A for an example program (courtesy Jeff Spooner). Data generated by MATLAB can be stored, transferred and reloaded into MATLAB using files with the .mat suffix. For example, load and plot the canine electrocardiogram (this file will not display in your browser window properly... right click on the link and "save link as" to your home account) stored in the vector "ecg" in a file named canine.mat.
>> load canineYou can use the save command to save data files while working in MATLAB (see help save).   | |||||||||||||||||||||||||
|   | 5. Getting Help | ||||||||||||||||||||||||||
|   |
On-line documentation is MATLAB is available using the help
command. To have the screen scroll through long help files, use
more on. To exit a scrolling help file, hit q. To
disable the scrolling, use more off.
>> more on   | ||||||||||||||||||||||||||
|   | 6. M-files | ||||||||||||||||||||||||||
|   |
MATLAB puts many commands at your disposal. Additionally, you can
create your own commands or programs. You may wish to write a MATLAB
program whenever you anticipate executing a sequence of statements
several times or again at a later session. to create your own MATLAB
program, use your favorite text editor and save the file with
extension .m in the directory here you will run MATLAB (see
help chdir). You may execute your m-file by typing the
filenames (without the .m extension) at the command prompt
(>>). There are two kinds of m-files: script files and
functions.
  | ||||||||||||||||||||||||||
|   |   | i. Script Files | |||||||||||||||||||||||||
|   |   |
Executing a script file is exactly like typing the commands it
contains at the command prompt. This is useful in executing a sequence
of commands while composing and debugging an m-file.
  | |||||||||||||||||||||||||
|   |   | ii. Function Files | |||||||||||||||||||||||||
|   |   |
Functions have designated input and output variables. Any other
variables used within a function are local variables, which do not
remain after the function terminates. Many of the functions supplied
in MATLAB are actually m-file functions. For example
>> type sinc
For example, the sinusoid signal in Section VI, Subsection 1 was generated without loops.   | |||||||||||||||||||||||||
|   |   | iii. Background Processes | |||||||||||||||||||||||||
|   |   |
With M-files it is now possible to run a MATLAB job in the background.
The synctax for running a job in the background is:
nice +20 matlab < inputfile > outputfile &   | |||||||||||||||||||||||||
|   | 7. Controls Toolbox | ||||||||||||||||||||||||||
|   |
The control toolbox contains a collection of MATLAB files designed for
solving controls and linear systems problems. For reference, Appendix B contains a categorized listing of
available commands. This list is available on-line
>> more on; help controlInteresting demonstration routine are also available
>> help ctrldemos   | ||||||||||||||||||||||||||
|   | 8. Signal Processing Toolbox | ||||||||||||||||||||||||||
|   |
The signal processing toolbox contains a collection of MATLAB files
designed for designing discrete and continuous time filters,
filtering, statistical signal processing, spectrum analysis, and
linear systems problems. For reference, Appendix C contains a categorized listing of
available commands. This list is available on-line
>> more on; help signalInteresting demonstration routines are also available
>> help SIGDEMOS   | ||||||||||||||||||||||||||
|   | 9. Finishing | ||||||||||||||||||||||||||
|   |
To exit MATLAB
>> quitHaving quit MATLAB, delete any files you no longer need using the rm command.   | ||||||||||||||||||||||||||
|   | A. Using MATLAB to Plot Data Generated by Other Programs | ||||||||||||||||||||||||||
|   |
For the C language (courtesy Jeff Spooner):
/* This file may be compiled using the format:
gcc filename.c -o exefile -lm
Running this program creates a file named "outvar.dat". The
data for a cosine and sine function is calculated and saved
as three columns of numbers.
To plot the data within MATLAB, you may use the following
from a MATLAB window:
>> load outvar.dat
>> x = outvar(:,1);
>> y = outvar(:,2);
>> z = outvar(:,3);
>> plot(x,y)
>> hold on
>> plot(x,z)
>> hold off
*/
#include
Comment:For Other High Level Languages: If you want to use Fortran, Pascal, Basic, or some other language and MATLAB to generate plots you proceed in a similar manner to how you do for C. Note that any space delimited ASCII data file can be read by MATLAB (i.e. columns of data separated by any number of "blank" spaces). Hence, if you use any high level language simply output your data into an ASCII file and use the MATLAB commands given above. Press here to view the matlab.c source code. To save the file, select "Save As" from the "File" menu of netscape.   | ||||||||||||||||||||||||||
|   | B. Man Pages | ||||||||||||||||||||||||||
|   | |||||||||||||||||||||||||||
EE281 homepage |
Vincent Juodvalkis - juodvalkis.1@osu.edu
February 10, 2003 - 12:56:10EST