Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Aaron Erhardt
Cheatsheets
Commits
13f07540
Commit
13f07540
authored
May 03, 2021
by
Aaron Erhardt
Browse files
Matlab lecture 3.5
Signed-off-by:
Aaron Erhardt
<
aaron.erhardt@t-online.de
>
parent
7b01dae4
Pipeline
#496
passed with stage
in 1 minute and 42 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Matlab/Matlab.md
View file @
13f07540
...
...
@@ -134,16 +134,17 @@ Histogram
```
matlab
histogram
(
A
)
histogram
(
A
,
15
)
% place values in 15 groups
```
## Images
Load image
###
Load image
```
matlab
img
=
imread
(
'path_to_file'
)
```
###
Show image
```
matlab
...
...
@@ -159,14 +160,14 @@ colormap(M) % where M 255x3 with values from 0 to 1
## Strings
Create
###
Create
```
matlab
str
=
"Hello"
fromChar
=
string
(
text
)
```
Concatenate
###
Concatenate
```
matlab
str2
=
str
+
" World!"
...
...
@@ -174,28 +175,26 @@ str2 = str + " World!"
## Character array
Create
###
Create
```
matlab
text
=
'Hello'
fromStr
=
char
(
str
)
```
Concatenate
###
Concatenate
```
matlab
text2
=
[
text
' World!'
]
```
Access characters
###
Access characters
```
matlab
c
=
text
(
1
)
% first character
```
### Convert
string to number
### Convert to number
```
matlab
str2num
(
'2'
)
...
...
@@ -203,19 +202,19 @@ str2num('2')
## Output
Create formatted string
###
Create formatted string
```
matlab
sprintf
(
'value: %f'
,
float_val
)
```
Print string
###
Print string
```
matlab
disp
(
'Hello'
)
```
Print formatted string
###
Print formatted string
```
matlab
% use \n for newline, the 1 is for printing to stdout
...
...
@@ -225,9 +224,7 @@ fprintf(1, 'value: %d\n', 2)
# Datatypes
## Structs
Create struct, add fields and access fields
## Struct
```
matlab
student
.
name
=
"TestStudent"
% create
...
...
@@ -240,19 +237,19 @@ disp(student.name) % print name
Array containing different data types
Create
###
Create
```
matlab
ca
=
{
'chars'
,
9.81
,
1
}
% 1x3 cell array
```
Access elements
###
Access elements
```
matlab
ca
(
1
)
% -> 'chars'
```
L
ength
### Get l
ength
```
matlab
length
(
ca
)
...
...
@@ -260,26 +257,26 @@ length(ca)
## Table
Properties
###
Properties
```
matlab
table
.
Properties
table
.
Properties
.
VariableNames
{
1
}
% first column name
```
Access column by name
###
Access column by name
```
matlab
table
.
column_name
% access column (as vector) with name "column_name"
```
Get sub-table
###
Get sub-table
```
matlab
table
(:,
2
)
% second column as table
```
Access values
###
Access values
```
matlab
table
{
1
,
2
}
% row 1, column 2, note brackets!
...
...
@@ -291,8 +288,6 @@ table{:, 2} % column 2
## Workspace
Save and load
```
matlab
save
file_name
load
file_name
...
...
@@ -300,8 +295,6 @@ load file_name
## Table
Load
```
matlab
table
=
readtable
(
'file_name'
)
detectImportOptions
(
'file_name'
)
% return detected import options
...
...
@@ -313,7 +306,6 @@ detectImportOptions('file_name') % return detected import options
matrix
=
readmatrix
(
'file_name'
)
% like readtable but only values as matrix
```
## File
```
matlab
...
...
@@ -321,6 +313,7 @@ file_handle = fopen('file_name', 'rw') % open as readable and writable
fclose
(
file_handle
)
```
# Polynomials
## Fit polynomial to points
...
...
@@ -377,6 +370,13 @@ semilogx(x, y); % x-axes is logarithmic, y-axis is linear
semilogy
(
x
,
y
);
% x-axes is linear, y-axis is logarithmic
```
### Simple function plot
```
matlab
fplot
(
@
sin
);
% default, from -5 to 5
fplot
(
@
sin
,
[
-
pi
pi
]);
% from -pi to pi
```
## Charts
### Bar
...
...
@@ -520,6 +520,110 @@ handle.Children(1).LineWidth = 2; % get line from handle and change width
```
# Functions
## Regular functions
### Definition
```
matlab
% global function, always at the top of a file
function
file_name
()
% code...
end
% local function
function
FuncName
()
% code...
end
```
### Parameters
```
matlab
function
funcName
(
param1
,
param2
)
disp
(
param1
);
% print param1
disp
(
param2
);
% print param2
end
```
Variable parameters
```
matlab
function
funcName
(
param1
,
param2
,
varargin
)
% function takes at least two parameters
for
cnt
=
1
:
length
(
varargin
)
arg
=
varargin
{
cnt
};
% get one of the variable arguments
if
isnumeric
(
arg
)
% only print numeric values
disp
(
arg
);
% print variable parameters
else
% ignore non-numeric values
disp
(
"Can't display non-numeric values"
);
end
end
end
```
### Return values
```
matlab
function
returnValue
=
duplicate
(
param
)
returnValue
=
[
param
param
];
end
value
=
duplicate
(
2
)
% value = [2 2]
% multiple return values
function
[
returnValue1
,
returnValue2
]
=
funcName
(
param1
,
param2
)
returnValue1
=
param1
;
returnValue2
=
param2
;
end
[
value1
,
value2
]
=
funcName
(
2
,
3
)
% value1 = 2, value2 = 3
```
### Documentation
```
matlab
function
funcName
()
% This is a documentation comment
% Explain what your function does here
% This is a normal comment again
end
```
## Anonymous functions
### Get function handle
```
matlab
handle
=
@
sin
handle
(
1
)
% returns sin(1)
```
### Define anonymous function
```
matlab
handle
=
@
(
param
)
2
*
cos
(
param
*
2
)
% handle(1) returns 2*cos(2)
handle
=
@
(
x
)
exp
(
x
-
x
.^
2
)
-
0.5
```
## Function characteristics
### Zeros
```
matlab
fzero
(
handle
,
1
)
% find a zero value starting from 1
```
### Fit parameters
```
matlab
id
=
@
(
is
,
ud
)
is
*
exp
(
ud
/
0.05
)
% diode current
is
=
lsqcurvefit
(
id
,
1e-10
,
u
,
i
);
% find ideal value for is to fit measured values u and i
```
# Control structures
## for-loop
...
...
@@ -584,6 +688,7 @@ switch value
end
```
# Performance
## Time measurment
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment