You are on page 1of 19

Matlab

Adriana Hera, Ph.D.

ahera @wpi.edu

- 2013-

Reference: Matlab documentation

MATLAB Course

1. Getting started; Matlab Help;

2. Variables;
3. Operators; 4. Matlab functions; 5. Matrices; 6. Scripts; 7. Basic plotting. 8. User Defined Functions 9. Importing Data

10. Simulink
11. Flow Control Commands
2

Matlab - Plotting plot Sintax: plot(y); plot(x,y); plot(x,y,s)

The plot function has different forms, depending on the input arguments.
If y is a vector, plot(y) produces a piecewise linear graph of the elements of y versus the index of the elements of y. If you specify two vectors as arguments, plot(x,y) produces a graph of y versus x.
3

Matlab - Plotting plot(x,y, s);

s
b g r c m y k blue green red cyan magenta yellow black

allows to plot : colors, symbols, different lines

. o x + * s d .

point circle x-mark plus star square diamond

: -. --

solid dotted dashdot dashed

(none) no line

plot (x,y,'c+:')

plots a cyan dotted line with a plus at each data point;


4

Matlab - Plotting
clear t=0:0.01:10; % time seconds signalSin=sin(2*pi*t); % signal1 - frequency =1 Hz signalCos=0.5*cos(2*pi*t); % signal2 - frequency =1 Hz figure plot(t,signalSin); hold on plot(t,signalCos, '-*r'); xlabel('time'); ylabel('signal'); legend('Sin', 'Cos'); title('Two Signals','FontSize',12)

plot2signals.m

Other commands:

xlabel figure ylabel legend, title


5

Matlab - Plotting
Two Signals
1 0.8 0.6 0.4 0.2 Sin Cos

signal

0 -0.2 -0.4 -0.6 -0.8 -1

5 time

10

Visualization - Interactive editing


show plot tools

Visualization - subplot
>> subplot(3,1,1)
1 0.5
0.8

>> subplot(1,3,1)
1

0.2

0.4

0.6

0.8

1
0.6

0.4

0.2

0.5

>> subplot(3,2,4) 1
1

subplot(m,n,q) * breaks the figure into a m x n matrix of windows.

2 4
0 0.5 1

q = the current window


3
0.5 0

plot3Windows.m
5 6
8

Visualization - subplot
clear; % clear the workspace close; % close previous figures
SignalSin 1

% Generate the signals


signal

t=0:0.02:10; % time seconds

signalSin=sin(2*pi*t); % signal1 - frequency =1 Hz


-1

signalCos=0.5*cos(2*pi*t); % signal2 - frequency =1 Hz


0 1 2 3 4 5 6 time(s) SignalCos 7 8 9 10

signal3=signalSin.*signalCos;

0.5
signal

% Plot the signals figure; subplot(3,1,1) plot(t,signalSin,'-ok', 'MarkerSize',2);

-0.5

title('SignalSin')
0 1 2 3 5 6 time(s) Product of two signals 4 7 8 9 10

xlabel('time(s)');

ylabel('signal');

0.2
signal

subplot(3,1,2) plot(t,signalCos, '-*g');

0 -0.2 0 1 2 3 4 5 time(s) 6 7 8 9 10

title('SignalCos') xlabel('time(s)'); ylabel('signal');

subplot(3,1,3) plot(t,signal3); grid title('Product of two signals') xlabel('time(s)'); ylabel('signal');

plot3Windows.m

axis([0 10 -0.3 0.3]); % sintax: axis([XMIN XMAX YMIN YMAX])

1.1 Importing and Exporting Data

1.1.2 Supported File Formats

10

1. Importing and Exporting Data

using the Import Wizard


save , load

dlmread , dlmwrite
xlsread, xlswrite fopen, , fscanf, fprintf

11

1.1 Importing and Exporting Data


1.1.1 Using the Import Wizard with Text Data File Import Data or >> uiimport

1.1.2 Supported File Formats


Wizard: missing data: NaN (Not-a-Number.) 12

1.1 Importing & exporting data: dlmread & dlmwrite


dlmread , dlmwrite Read/Write ASCII delimited file.

prepareData.m

fileName
data=dlmread(myFile);

matrix to be saved

add at the end of existing file

dlmwrite(fileName, data,'delimiter', '\t', '-append'); dlmwrite(fileName, data, '\t')

Delimiter = tab

data=dlmread('dataCSV.csv', ',',2,0);
dlmread reads from the ASCII-delimited numeric data file filename to output matrix M. The delimiter separating data elements is inferred from the formatting of the file.
Comma (,) is the default delimiter.
13

1.1 Importing & exporting data: dlmread & dlmwrite


dlmread , dlmwrite Read/Write ASCII delimited file.

for i=1:4 fileName=['results', num2str(i), '.csv'] data(i,:,:)=dlmread(fileName, ',',2,0); end

14

1.1 Importing & exporting data: xlswrite, xlsread

xlswrite, xlsread - Write/Read Excel file.


xlswrite('filename', data) xlsread('filename')

To import Excel data by wizard (uiimport) the file should have the extension .xls

15

Fourier Transform
MATLAB provides a collection of functions for computing and working with Fourier transforms.

Y = fft(X)
returns the discrete Fourier transform (DFT) of vector X, computed with a fast Fourier transform (FFT) algorithm.

Example: open the file myFFT.m

Other related commands: fft(X,N) is the N-point FFT ifft, abs, angle, unwrap HT hilbert transform myHT.m
16

17

Fourier Transform

Example: open the file myFFT.m

Other related commands: fft(X,N) is the N-point FFT ifft, abs, angle, unwrap HT hilbert transform myHT.m
18

>> wintool

19

You might also like