MATLAB Tutorial: Comprehensive Guide for Signal Processing and Math Computations - Prof. D, Lab Reports of Electrical and Electronics Engineering

This matlab tutorial provides an overview of matlab, its functionality, and how to obtain and use it. Learn about matrices, data types, built-in functions, and user-defined functions. Discover alternatives to matlab, and explore the use of matrices, accessing individual elements, finding the size of variables, and determining variables in the workspace. This tutorial also covers creating figures, plotting continuous and discrete-time signals, labeling plots, creating multiple graphs, and saving and loading data.

Typology: Lab Reports

Pre 2010

Uploaded on 09/17/2009

koofers-user-ags
koofers-user-ags 🇺🇸

8 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB Tutorial
Dr. David W. Graham
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download MATLAB Tutorial: Comprehensive Guide for Signal Processing and Math Computations - Prof. D and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity!

MATLAB Tutorial

Dr. David W. Graham

What is MATLAB?

• MATLAB – Mat

rix Lab

oratory

• The premier number-crunching software• Extremely useful for signal processing

  • Has many built-in functions useful for learning

signals and systems

Obtaining and Using MATLAB

-^

MATLAB can be found on many computers in thedepartment and ESB^ – ESB 813 has MATLAB on 23 computers– MATLAB is in many of the computer labs on the

second floor of ESB

-^

Purchasing MATLAB^ –

www.mathworks.com – Student version available for $99– Need the Control Systems Toolbox ($29)

MATLAB Alternatives (FREE)

• Octave

www.gnu.org/software/octave

• Scilab

www.scilab.org

Accessing Individual Elements of a Variable

-^

Access individual elements in a matrix^ – A(

row

col

  • e.g.

A(1,2)

returns a value of

-^

For arrays that are single row or column matrices(called vectors), you only need one position^ – tt(5)

-^

Accessing an entire row or column^ – Use a colon to indicate all elements within a row or

column

  • A(:,2)

returns all rows in the second column

  • This would return

Finding the Size of a Variable

• size(variable_name)• size(A)• size(tt) • Returns the size in terms of rows and

columns

Use of Matrices

MATLAB inherently works with matrices•^

All operations default to matrix operations

-^

Let

A = [ 1 2 3; 4 5 6];

-^

Let

B = [1; 1; 1];

-^

C = A*B^ –

Returns

6; 15

-^

If you tell MATLAB to multiply, it assumes you mean matrixmultiplication

-^

The

mark performs the transpose of the matrix

-^

Ex.

A’

returns

1 4; 2 5; 3 6

For element-by-element operation (multiplication, division,

exponentiation, etc.)

-^

Use the “.” operator – Put a. before the operator

-^

Ex.

tt_squared = tt.*tt;

-^

This also works with division (

/) and exponentiation (

^)

Creating Figures

Figures are used to graphically display data• To create a new figure, type

figure

• To create a specific figure number, type

figure(number);

• Ex.

figure(10);

• MATLAB will always plot to the most

recently selected figure

Plotting

Plotting discrete-time signals • stem(independant_variable,dependant_

variable);

-^

Ex.

stem(tt,yy);

-^

You can change the color of the markers with theoptional argument

stem(tt,yy,'r')

-^

Can add a second plot to the same figure by runningthe command

hold on;

-^

Type

help stem

for more options

Labeling Plots

• You are able to label the axes and title your

figures with text strings

• You must use text strings offset by

marks

within the appropriate function^ – Labeling your plots

  • xlabel('Time (s)')• ylabel('Output')
    • Titling your plots
      • title('The Example Signal')

Other Useful Plotting Commands •^

Create a legend for all the traces on the plot^ –

legend('Signal 1', 'Signal 2')

-^

Change the axes ranges to display only the range youdesire^ – axis([low_x high_x low_y high_y])^ –

Ex.

axis([-10 10 0 5]);

•^

Turn the grid on or off^ – grid

Toggles the grid on or off

  • grid on

Turns the grid on

  • grid off

Turns the grid off

•^

Hold the plot to allow another signal to be plotted on thesame plot^ – hold

Toggles the hold function

  • hold on

Turns the hold function on

  • hold off

Turns the hold function off

Printing and Saving Your Plots

•^

Plots can be printed directly from the figure window

-^

Plots can also be saved in a variety of formats^ –

.fig files

-^

.fig files are the default from the pull-down menu

-^

.fig files can only be reopened in MATLAB

-^

Metafile for use in Word documents and PowerPoint slides^ •

Copies the figure to the clipboard • print –dmeta

-^

.eps files^ •

E

ncapsulated P

ost S

cript files

-^

Vectorized images

-^

For use in LaTeX files

-^

Called file_name.eps

  • print -dmeta file_name

Saving Your Data

You are able to save all of your data in the workspace using

the

save

function

  • save file_name variable_1 variable_2... •^

Ex.

save ee327_example A tt

•^

Creates a file called

ee327_example.mat

You can later load these variables by using the

load

function

  • load file_name •^

Ex.

load ee327_example

will load all variables

contained in the .mat file (

A

and

tt

•^

This command will replace any existing variables of thesame name that are already in the workspace

Use of Scripts

Instead of entering everything at the command line, you can

write scripts (.m files) to allow you to save your steps Uses the same syntax as the command line•^

To create a new script, type

edit

at the command line

•^

To edit an existing script, type edit filename The

symbol comments everything following it on the line