Octave/Matlab Tutorial: Kai Arras
Octave/Matlab Tutorial: Kai Arras
Kai Arras
Social Robotics Lab
This tutorial:
This tutorial applies to Octave *and* Matlab
unless stated otherwise!
octave:1>
Creating a Structure
Type for instance:
octave:5> data.id = 3;
octave:6> data.timestamp = 1265.5983;
octave:7> data.name = 'sensor 1 front';
Variables and Data Types
Creating a Array of Structures
Oh, a new measurement arrives. Extend struct by:
octave:8> data(2).id = 4;
octave:9> data(2).timestamp = 1268.9613;
octave..> data(2).name = 'sensor 1 front';
Suppress Output
Add a semicolon:
octave:2> a;
octave:3> sin(phi);
Alternative Example:
octave:2> phi = pi/3;
octave:3> R = [cos(phi) -sin(phi); sin(phi) cos(phi)]
R =
0.50000 -0.86603
0.86603 0.50000
Matrices
Creating a Matrix from Matrices
octave:1> A = [1 1 1; 2 2 2]; B = [33; 33];
Column-wise
octave:2> C = [A B]
C =
1 1 1 33
2 2 2 33
Row-wise:
octave:3> D = [A; [44 44 44]]
D =
1 1 1
2 2 2
44 44 44
Matrices
Indexing
Always "row before column"!
aij = A(i,j) Get an element
r = A(i,:) Get a row
c = A(:,j) Get a column
B = A(i:k,j:l) Get a submatrix
Adding a Row/Column
If the referenced row/colum doesn't exist, it's added.
octave:3> A(4,:) = 4
A =
1 2 3 4 5
2 2 2 2 2
-3 -3 -3 -3 -3
4 4 4 4 4
Matrices
Deleting a Row/Column
Assigning an empty matrix [] deletes the
referenced rows or columns. Examples:
octave:4> A(2,:) = []
A =
1 2 3 4 5
-3 -3 -3 -3 -3
4 4 4 4 4
octave:4> A(:,1:2:5) = []
A =
2 4
2 2
-3 -3
4 4
Matrices
Get Size
nr = size(A,1) Get number of rows of A
nc = size(A,2) Get number of columns of A
[nr nc] = size(A) Get both (remember order)
l = length(A) Get whatever is bigger
numel(A) Get number of elements in A
isempty(A) Check if A is empty matrix []
Octave only:
nr = rows(A) Get number of rows of A
nc = columns(A) Get number of columns of A
Matrices
Matrix Operations
B = 3*A Multiply by scalar
C = A*B + X - D Add and multiply
B = A' Transpose A
B = inv(A) Invert A
s = v'*Q*v Mix vectors and matrices
d = det(A) Determinant of A
[v lambda] = eig(A) Eigenvalue decomposition
[U S V] = svd(A) Sing. value decomposition
Examples:
[m n l] = size(A)
A = rand(m,n,l)
m = min(min(min(A)))
aijk = A(i,j,k)
A(:,:,5) = -3
Matrices
Matrix Massage
reshape(A,m,n) Change size of matrix A to
have dimension m x n. An
error results if A does not
have m x n elements
Example:
octave:1> x = linspace(0,2*pi,100);
octave:2> plot(x,cos(x),'r+',x,sin(x),'bx');
plot(x,cos(x),'r+',x,sin(x),'bx');
Plotting
Adjusting the axes
octave:3> axis([0 2*pi -1 1])
plot(x,cos(x),'r+',x,sin(x),'bx');
Plotting
Uhm..., don't like it. New try:
octave:1> clf;
Adding Text
octave:4> text(1,-0.5,'cos(\phi)')
octave:5> text(3,0.5,'sin(\phi)')
plot(x,cos(x),'r+',x,sin(x),'-x','Color',[1 .4 .8],'MarkerSize',2)
Plotting
Yepp, I like it... Get hardcopy!
Exporting Figures
print –deps myPicBW.eps Export B/W .eps file
print –depsc myPic.eps Export color .eps file
print –djpeg –r80 myPic.jpg Export .jpg in 80 ppi
print –dpng –r100 myPic.png Export .png in 100 ppi
Octave/Matlab is case-sensitive.
Text Editors
Use an editor with m-file syntax highlighting/
coloring.
Programming
Control Structures
if Statement
if condition,
then-body;
elseif condition,
elseif-body;
else
else-body;
end
for statement
for var = expression,
body;
end
Programming
Interrupting and Continuing Loops
break
Jumps out of the innermost for or while loop that
encloses it.
continue
Used only inside for or while loops. It skips over
the rest of the loop body, causing the next cycle to
begin. Use with care.
Programming
Increment Operators (Octave only!)
Simply write
function [ret-var] = name(arg-list)
body
end
function an = normangle(a,mina);
if a < Inf,
[...]
help text
Functions and Scripts
Setting Paths
save my_vars.mat
Saves all current variables into file my_vars.mat
save results.mat resultdata X Y
Saves variables resultdata, X and Y in file results.mat
save ... -ascii
Saves variables in ASCII format
save ... -mat
Saves variables in binary MAT format
Files I/O
Load Variables
The corresponding command is load.
load my_vars.mat
Retrieves all variables from the file my_vars.mat
load results.mat X Y
Retrieves only X and Y from the file results.mat
Read/write images
imread Read image from file (many formats)
imwrite Write image to file (many formats)
Contents
Overview
Start, quit, getting help
Variables and data types
Matrix arithmetic
Plotting
Programming
Functions and scripts
Files I/O
Misc
Octave and Matlab in practice
librobotics
Misc
Cleaning Up
clear A Clear variable A
clear frame* Clear all variables whose names
start with frame...
clear Clear all variables
clear all Clear everything: variables,
globals, functions, links, etc.
Tab completion
Octave/Matlab have tab completion. Type some
letters followed by tab to get a list of all commands
that start with the letters you typed.
Misc
Built-in Unix Commands
pwd Display current working directory
ls List directory. See also dir .
cd Change directory
mkdir Make new directory
rmdir Delete directory
Related Commands
movefile Move file
copyfile Copy file
Misc
Random Seeds
rand and randn obtain their initial seeds from the
system clock.
To generate identical/repeatable sequences, set the
random generator seeds manually.
Making animations
Calling unix/dos functions from within Octave
programs
Increasing speed
Octave/Matlab in Practice
Output Files in Octave Syntax
Write:
A = zeros(100,50); % preallocate matrix
for i = 1:100,
A(i,:) = rand(1,50);
end;
Octave/Matlab in Practice
Speed: Structure of Arrays
Array of structure
people(1).name = 'Polly J Harvey';
people(1).age = 32;
people(2).name = 'Monica Lebowski';
people(2).age = 27;
Contents
Overview
Start, quit, getting help
Variables and data types
Matrix arithmetic
Plotting
Programming
Functions and scripts
Files I/O
Misc
Octave and Matlab in practice
librobotics
librobotics
librobotics is a small library with frequently used
Octave/Matlab functions in Robotics, especially for
visualization.
Kai Arras
Social Robotics Lab