Laboratory Work Preparation: Lab Work 1: Introduction To Matlab
Laboratory Work Preparation: Lab Work 1: Introduction To Matlab
MATLAB
Matrix laboratory
MATLAB
Environment
Current folder
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 4 / 25
MATLAB
Help
Description
of usage
Keyword search
Examples
of usage
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 5 / 25
MATLAB
Literature
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 6 / 25
s = v = v =
10 10 20 30 10
20
>> ndims(s) 30
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 7 / 25
VARIABLES
Matrix
>> M = [10, 20, 30; 40, 50, 60] >> M = [10, 20, 30; 40, 50]
??? Error using ==> vertcat
M = CAT arguments dimensions are not
10 20 30 consistent.
40 50 60
>> M = []
>> M(5)
M =
ans = []
30
>> isempty(M)
>> M(2,3)
ans =
ans = 1
60
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 8 / 25
VARIABLES
Multidimensional array
>> P(:,:,1) = [10, 20, 30; 40, ... >> P(2,3,1) >> P(end,2,end)
50, 60]
ans = ans =
P = 60 110
10 20 30
40 50 60 >> P(:,2:3,1) >> P(:)
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 9 / 25
VARIABLES
String
ans =
Biomedical informatics
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 10 / 25
VARIABLES
Cell
c = ans =
[2x3 double] 1 2 3
4 5 6
>> c{1,2} = [1, 2; 3, 4; 5, 6]
>> c{1,1}(:,2)
c =
[2x3 double] [3x2 double] ans =
2
>> c{2,1} = 'word' 5
c = >> c{2,2}
[2x3 double] [3x2 double]
'word‚ [] ans =
[]
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 11 / 25
OPERATIONS
Arithmetic operations
B = ans = ans =
1 2 2 4 1 4
3 4 6 8 27 256
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 12 / 25
OPERATIONS
Logical operations
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 13 / 25
OPERATIONS
Some useful operations on variables
Matrix size Steps Initial values
>> m = [1, 2, 3; ... >> v1 = 1:6 >> zeros(2, 3)
4, 5, 6];
>> size(m) v1 = ans =
1 2 3 4 5 6 0 0 0
ans = 0 0 0
2 3 >> v2 = 1:2:6
>> ones(2, 3)
>> length(m) v2 =
1 3 5 ans =
ans = 1 1 1
3 >> v3 = 1:3:6 1 1 1
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 14 / 25
OPERATIONS
Some useful operations on variables
Rounding Average, maximum, minimum Standard deviation
>> pi >> m = [1, 2, 3; ... >> std(m)
4, 5, 6]
ans = ans =
3.1416 m = 2.1213 2.1213 2.1213
1 2 3
>> round(pi) 4 5 6 >> std(std(m))
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 15 / 25
COMMANDS
Iterative loops: for / while
s = s = s =
0 0 2
s = s = s =
1 2 4
s = s = s =
3 6 6
s = s =
6 8
s = s =
10 10
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 16 / 25
COMMANDS
Conditionals: if / if-else / if-elseif-else / switch-case-otherwise
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 17 / 25
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 18 / 25
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 19 / 25
>> Summa()
ans =
0
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 20 / 25
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 21 / 25
>> x = -pi:0.1:pi;
>> y1 = sin(x); y2 = cos(x);
Sine and cosine
>> plot(x, y1);
>> plot(x, y1, x, y2);
>> plot(x, y1, 'r-', x, y2, 'b:');
>> plot(x, y1, 'r-', x, y2, ...
'b:', 'LineWidth', 3);
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 22 / 25
h = Sine
Sine and cosine
Sine and
and cosine
cosine
182.0078
>> get(h)
...
FontSize = [10]
FontWeight = normal
...
>> h = xlabel('x');
>> set(h, 'FontSize', 12 , ...
'FontWeight', 'bold');
>> h = ylabel('y');
>> set(h, 'FontSize', 12 , ...
'FontWeight', 'bold');
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 23 / 25
VISUALIZATION
Customizing the plot
>> get(gca)
... Sine and cosine
>> get(gcf)
...
Color = [0.8 0.8 0.8]
...
Name =
...
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 24 / 25
LABORATORY WORK
Lab Work 1: Introduction to Matlab
4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1
2 2 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 1 1 2 2
5 5 5 5 5 5 3 3 3 3 3 3 4 4 1 1 1 1 3 3 3 3
3 3 3 3 3 3 5 5 1 1 1 1 1 1 4 4 4 4 4 4 4 4
1 1 1 1 1 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5
1st pass 2nd pass 3rd pass 4th pass
(4 ×) (3 ×) (2 ×) (1 ×)
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 25 / 25
LABORATORY WORK
Lab Work 1: Introduction to Matlab
Element position
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course