You are on page 1of 26

MATLAB ® - The Language of Technical

Computing

Session #1: Introduction


to Matlab, basic data
manipulation and
computation, plotting,
numerical calculus. http://www.mathworks.com
What is MATLAB good for?
-Matrix calculations (MatLab= “Matrix” Lab)
-Manipulating and plotting data… especially large data
sets.
-Scientific computing.
-Dynamic system modeling and control system design.
-Much, much more…
With certain toolboxes:
- Optimization - Embedded coding
- Data acquisition - Simulation
- Image processing - Signal processing
Show some examples of Matlab code (This is motivational –
you are not expected to fully understand how this code
works now, I want you to see where we are going):

1. Solving for the constants in a heat transfer problem.


(HeatTransfer.m)
2. Modeling a MEMS microphone. (MEMS_mic.m)
3. Testing an acoustic holography algorithm
(AcousticHolography.m).
4. Plotting steady state vibration data from a LDV test on a
ultrasound element (VibeDataAnalysis.m).
Start Matlab… under “Engineering
and Science Applications” in the
START menu.
Matlab Help: lookfor and help
lookfor searches for related functions:

help gives specific information:


Matlab Help: lookfor and help
doc pops up a new window with help:
>> doc rlocfind
Defining and manipulating data:
EVERYTHING IS A MATRIX – a scalar just
happens to be a 1x1 matrix.

>> a = -9.5
a=
-9.5000

I have defined a 2 by 2 matrix called “A”. Semicolon separates


rows. Spaces separate columns. I used the previously defined
variable “s” (s=1.73+3.5*i)
Defining and manipulating data:

A is a 2 by 2 matrix. A11 = 1.731 + 3.5i

The conjugate transpose of A is


computed by the command A’.
It transposes the matrix and changes
every complex value to the complex
conjugate.
Defining and manipulating data:

This syntax allows me to Vectors are really just 1xn or nx1 matrices
select part of the matrix as far as Matlab is concerned.
(rows 1 to 2, column 1).

The command linspace is particularly useful for generating a linearly


spaced vector of values from a starting value to an end value with a given
number of elements. Here we start at 1, go to 5, and have 7 points.
Defining and manipulating data:

To refer to an entire column or row


of a matrix, you can just use the
colon operator with no numbers

Strings are defined using single quotes:

There are a bunch of string manipulating


functions. Type help strfun
Conditional indexing:
Instead of putting a specific index (like A(1:3,1)), you can
index based on a logical statement. For instance, say
you have a data set where time is in a vector t, and
voltage is in a vector, v:
>> t=linspace(0,1,1000);
>> v=sin(t.^2)
If I want to look at the mean value of v for times between
0.2 and 0.3 seconds,
>>mean(v(t>0.2 & t<0.3))

Here is the “logical index”… I am asking for the values of


v for which this conditional statement is true.
Conditional indexing:
As another example, let’s say you have a sine wave and
you want to make all the negative parts of the curve zero
(half-wave rectify the signal):
>> t=linspace(0,1,1000);
1

>> v=sin(10*t)
0.8

>> v(v<0)=0 0.6

0.4
Here is the “logical
index”… I am asking for 0.2
the values of v for which
this conditional 0
statement is true. 0 0.2 0.4 0.6 0.8 1
Saving and Loading Data

.mat files
You may save and load data from the workspace into .mat
files, which are Matlab data files, using the “save” and
“load” commands.

>> save mydata.mat


This will save all currently defined variables in the workspace
to a data file called mydata.mat in the current directory.

>> save mydata2.mat x y t


This will save only the variables x, y, and t in the workspace to
a data file called mydata2.mat in the current directory.
Saving and Loading Data
.csv files
Text data in a comma separated value (csv) file or just a
text file with tab separated data may be saved and loaded
from the workspace using “save –ascii” and “load”
commands.

>> S=load('test.csv')

S=

1.0000 -1.0000 0
2.0000 -2.0000 0.5000
3.0000 -3.0000 12.0000

One thing we often do is use Labview to


gather data and write it into a text file,
then read it into Matlab to analyze it.
Built in constants:

The variable
ans is the result
of the last
computation
and can be
used just like
any other
variable, but will
be overwritten.

The
command
clear clears
all defined
variables.
Built in functions:

Everything you would expect. All trig functions work in radians.


Log base 10 is called log10. Log base e is called log.
Particularly useful for complex #s:
abs(s) = magnitude of s
angle(s) = phase of s
The DOT operator:

A*A is matrix multiplication.


A.*A is element-by-element multiplication.
Same idea for division (/ vs. ./), and
power (^ vs .^).
Plotting in Matlab: See “help plot” for more options.
Can also edit curves and figure
format using the mouse like a
you would expect.

I created a 1000 element


vector called “t” which goes
from 0 to 1. The semicolon at
the end of the line stops
Matlab from printing out the
long vector. I computed x
based on t. Then I plotted x
vs. t. A figure window pops
up.
Plotting in Matlab:
To open a new figure windows, type:
>> figure
To make figure 2 current, type:
>> figure(2)
Matlab will plot on the current figure. If there is
already a plot there, it will be overwritten, unless
you first tell Matlab to hold the plot:
>> hold on
To turn off the hold,
>> hold off
Plotting in Matlab:
•To put a Matlab figure into your Word document or
webpage or whatever, you can either go to the “edit”
menu on the figure window and say “Copy Figure” and
then paste into Word or Powerpoint – or, you can go to
the “File” menu and save the figure as a .jpg, .gif, .bmp,
.eps, etc.
• Matlab figures are saved as “.fig” files, which can be
opened in Matlab and you can change formatting etc.
• Also, note that you can copy and past data from one
figure to another – so if I select a curve in one figure,
copy it, and then click in another figure and paste it will
drop the curve in – very nice for comparing data sets, for
instance.
ginput – pulling data off a figure
Centerpoint Response to a 10V Step
2
To pull “n” points off a plot, “stepresp.fig”
type 1

>> ginput(n)

Displacement (nm)
0

You will get a crosshairs


on your current plot. -1

Wherever you click, you


-2
will get back the location.
-3
>> ginput(2)
-4 ans =
-1 0 1 2 3 4 5
Time (µ s) 0.3894 0.0088
For instance, I could determine 0.5691 -0.0439
the frequency of the resonance
seen in this experimentally >> T=ans(2,1)-ans(1,1)
measured step response by: T = 0.1797

>> f=1/T
f= 5.5641
2D Plotting in Matlab:

I created two 100x100


matrices, X and Y, using the
meshgrid function (see help
file). I computed the matrix Z
based on the X and Y
matrices. I plotted a surface
plot of Z vs. X and Y.

See help files on meshgrid


and surf for more
information.

pcolor is another useful 2D plotting command.


Numerical Calculus:
The gradient function can be used for numerical derivatives:

See help files on gradient,


subplot and plot.
Numerical Calculus:
The trapz and cumtrapz functions can be used for numerical integration:

See help files on trapz, and


cumtrapz.

You might also like