Commit 938f0a85 authored by Aaron Erhardt's avatar Aaron Erhardt
Browse files

Lecture 19.4


Signed-off-by: default avatarAaron Erhardt <aaron.erhardt@t-online.de>
parent d309fc2d
......@@ -218,7 +218,8 @@ disp('Hello')
+ Print formatted string
```matlab
fprintf('value: %d\n', 1) % use \n for newline
% use \n for newline, the 1 is for printing to stdout
fprintf(1, 'value: %d\n', 2)
```
# Datatypes
......@@ -253,7 +254,7 @@ ca(1) % -> 'chars'
+ Length
```matlab
lenght(ca)
length(ca)
```
## Table
......@@ -262,19 +263,25 @@ lenght(ca)
```matlab
table.Properties
table.Properties.VariableNames(1) % first column name
table.Properties.VariableNames{1} % first column name
```
+ Access column
+ Access column by name
```matlab
table.column_name
table.column_name % access column (as vector) with name "column_name"
```
+ Access elements
+ Get sub-table
```matlab
table(:, 2) % second column as table
```
+ Access values
```matlab
table{1, 2} % row 1, column 2
table{1, 2} % row 1, column 2, note brackets!
table{:, 2} % column 2
```
......@@ -298,6 +305,13 @@ table = readtable('file_name')
detectImportOptions('file_name') % return detected import options
```
## Matrix
```matlab
matrix = readmatrix('file_name') % like readtable but only values as matrix
```
## File
```matlab
......@@ -305,13 +319,117 @@ file_handle = fopen('file_name', 'rw') % open as readable and writable
fclose(file_handle)
```
# Polynomials
## Fit polynomial to points
```matlab
% x and y: coordinate values, deg: degree of polynomial
p = polyfit(x, y, deg);
```
## Calculate values
```matlab
% get y values for vector x and polynomial x
y = polyval(p, x);
```
# Graphics
## Plot
```matlab
x = 1:10;
y = x.^2;
plot(y); % automatic x-axis
plot(x, y); % custom x-axis
```
### Hold (keep previous plots)
```matlab
hold on
plot(...)
```
### Formatting
+ Markers
```matlab
plot(x, y, '.r'); % red dots
plot(x, y, 'or'); % red circles
plot(x, y, 'xr'); % red x
plot(x, y, '*r'); % red stars
```
+ Lines
```matlab
plot(x, y, '-'); % regular lines
plot(x, y, ':'); % dotted lines
plot(x, y, '--'); % dashed lines
plot(x, y, '-.'); % dashed and dotted lines
```
+ Colors
```matlab
plot(x, y, 'color', [0, 0.6, 0.2]); % RGB color
```
+ Line width
```matlab
plot(x, y, 'LineWidth', 2); % set line width to 2 px
```
+ Line width
```matlab
plot(x, y, 'MarkerFaceColor', [0, 0.6, 0.2]); % RGB color of markers
```
+ Line width
```matlab
plot(x, y, 'MarkerSize', 7); % set marker size to 7 px
```
### Legend and title
```matlab
title('Title');
% number of names should match number and order of plots
legend('First', 'Second', 'Third');
% legend with specific location
legend('First', 'Second', 'Third', 'Location', 'northwest');
```
### Labels
```matlab
xlabel('\alpha'); % set x-label to α
ylabel('y');
```
## Figures
```matlab
fig = figure
```
# Control structures
## for-loop
```matlab
for cnt = 1:5 % cnt can be any row vector
fprintf('%d\n', cnt);
fprintf(1, '%d\n', cnt);
end
```
......@@ -320,7 +438,7 @@ end
```matlab
cnt = 0;
while cnt < 5
fprintf('%d\n', cnt);
fprintf(1, '%d\n', cnt);
cnt = cnt + 1;
end
```
......@@ -352,3 +470,62 @@ if ~isequal(vec1, vec2)
disp('Not equal')
end
```
## switch-statement
```matlab
value = 4;
switch value
case 1
display("One");
case 2
display("Two");
case 3
display("Three");
otherwise
display("Other number");
end
```
# Performance
## Time measurment
```matlab
tic % start time measurement
time = toc % take time measurement
time2 = toc % take another time measurement (from same tic)
```
## Solving linear equations
```matlab
x = A\b; % fast, because optimized by Matlab
x = inv(A)*b; % slow
```
## Pre-allocating vectors
```matlab
for cnt = 1:100 % inefficient, causes a lot of re-allocations
a(cnt) = cnt;
end
b = zeros(1, 100); % pre-allocating memory
for cnt = 1:100
a(cnt) = cnt;
end
```
### Real-world example
```matlab
x = 0:0.01:150;
y = zeros(size(x)); % initialized with zeros, same size as x
cnt = 1;
for i = x
y(cnt) = sin(i);
cnt = cnt + 1;
end
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment