Matlab Tutorial
Matlab Tutorial
Matlab Basics
• To start Matlab: Select MATLAB on the menu (if
using Windows). Type “matlab” on the
command line (if using Linux).
Getting Help and
Looking Up Functions
• To get help on a function type “help function_name”,
e.g., “help plot”.
• To find a topic, type “lookfor topic”, e.g., “lookfor matrix”
Matlab’s Workspace
• who, whos – current workspace vars.
• save – save workspace vars to *.mat file.
• load – load variables from *.mat file.
• clear all – clear workspace vars.
• close all – close all figures
• clc – clear screen
• clf – clear figure
Basic Commands
• % used to denote a comment
• ; suppresses display of value (when
placed at end of a statement)
• ... continues the statement on next line
• eps machine epsilon
• inf infinity
• NaN not-a number, e.g., 0/0.
Numbers
• To change format of numbers:
format long, format short, etc.
See “help format”.
• Mathematical functions: sqrt(x), exp(x),
cos(x), sin(x), sum(x), etc.
• Operations: +, -, *, /
• Constants: pi, exp(1), etc.
Examples:
5+5
3^2
sin(pi /2)
732 * 20.3
============
x = 3;
y=x+5
============
A = 1:10;
S = sum(A)
=============
Command Purpose
1 int8
8-bit signed integer
2 uint8
8-bit unsigned integer
3 int16
16-bit signed integer
4 uint16
16-bit unsigned integer
5 int32
32-bit signed integer
6 uint32
32-bit unsigned integer
7 int64
64-bit signed integer
8 uint64
64-bit unsigned integer
9 single
single precision numerical data
10 double
double precision numerical data
11 logical
logical values of 1 or 0, represent true and false respectively
12 char
character data (strings are stored as vector of characters)
13 cell array
array of indexed cells, each capable of storing an array of a different dimension and data type
14 structure
C-like structures, each structure having named fields capable of storing an array of a different
dimension and data type
15 function handle
pointer to a function
16 user classes
objects constructed from a user-defined class
17 java classes
objects constructed from a Java class
Example
• str = 'Hello World!'
n = 2345
d = double(n)
un = uint32(789.50)
rn = 5678.92347
c = int32(rn)
=================
Operator Purpose
\ Left-division operator.
/ Right-division operator.
: Colon; generates regularly spaced elements and represents an entire row or column.
. Decimal point.
= Assignment operator.
Special Variables and Constants
MATLAB supports the following special variables and constants −
Name Meaning
Here m = 2 and n = 3.
Graphics (3)
• plot3(x,y,z) % plot 2D function.
• mesh(x_ax,y_ax,z_mat) – surface plot.
• contour(z_mat) – contour plot of z.
• axis([xmin xmax ymin ymax]) – change
axes
• title(‘My title’); - add title to figure;
• xlabel, ylabel – label axes.
• legend – add key to figure.
Examples of Matlab Plots
Examples of Matlab Plots
Examples of Matlab Plots
File Input/Output
• fid = fopen(‘in.dat’,’rt’); % open text
file for reading.
• v = fscanf(fid,’%lg’,10); % read 10
doubles from the text file.
• fclose(fid); % close the file.
• help textread; % formatted read.
• help fprintf; % formatted write.
Example Data File
Sally Type1 12.34 45 Yes
Joe Type2 23.54 60 No
Bill Type1 34.90 12 No
Read Entire Dataset
fid = fopen(‘mydata.dat’, ‘r’); % open file
for reading.
diary filename
x=3
diary off
tic
commands…
toc
% Backward summation
backwardsum = 0;
for i=10:-1:1
backwardsum = backwardsum+1/(i^2);
end;
Interactive Example (2)
• Write a Matlab function to multiply two
n-by-n matrices A and B. (Do not use
built-in functions.)
Solution
function [C] = matrix_multiply(A,B,n)
C = zeros(n,n);
for i=1:n
Can this code be written so that it
for j=1:n
runs faster?
for k=1:n
C(i,j) = C(i,j) + A(i,k)*B(k,j);
end;
end; Hint: Use vectorization.
end;
Solution
• Script to use for testing:
n = 10;
A = rand(n,n);
B = rand(n,n);
C = matrix_multiply(A,B,n);