MATLAB basic
What is Matlab?
Matlab Screen
Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities
Flow Control
Using of M-File
Writing User Defined Functions Conclusion
Variables
No need for types. i.e.
int a;double b;float c;All variables are created with double precision unless specified and they are matrices.
Example:
x=5;
x1=2;
a vector
1 | x = [1 2 5 1] |
x=
1 2 5 1 不加分号, 按下回车就输出
a matrix
x = [1 2 3; 5 1 4; 3 2 -1]; If you include “;” at the end of each statement, result will not be shown immediately
long array, matrix
1 | t =1:10 |
t=
1 2 3 4 5 6 7 8 9 10
1 | t = 2: -0.5: -1 |
t =
2.0000 1.5000 1.0000 0.5000 0 -0.5000 -1.0000
1 | t = [1:4; 5:8] |
x= 1 2 3 4
5 6 7 8
Generating Vectors from functions
1 | x = zeros(1,3) |
x=
0 0 0
1 | x = ones(1,3) |
x=
1 1 1
1 | x = rand(1,3) |
ouput: 1 column 3 numbers
x=
0.9501, 0.2345, 0.3044
Concatenation of Matrices
1 | x = [1 2], y = [4 5], z=c[0 0] |
A = [x y]
1 2 4 5
B = [x ; y]
1 2
4 5
operand
.* element-by-element multiplication
./ element-by-element division
.^ element-by-element power
== Equal to
~= Not equal to
< Strictly smaller
__>__ Strictly greater
<= Smaller than or equal to >= Greater than equal to
& And operator
| Or operator
i.e.
Plot the function e-x/3sin(x) between 0≤x≤4π
Create an x-array of 100 samples between 0 and 4π
1
x = linspace(0, 4*pi, 100);
Calculate sin(.) of the x-array
1
y = sin(x);
Calculate e-x/3 of the x-array
1
y1 = exp(-x/3);
Multiply the arrays y and y1
1
y2 = y .* y1;
Plot the y2-array
1 | plot(y2); |
Display facilities
plot(y)
stem(y)
title(‘This is the sinus function’)
xlabel(‘x (secs)’)
ylabel(‘sin(x)’)
Control Structures
Output 1-200, the difference between each number is 3 i.e 1,4,7,10,13,16…..
For loop
for i=Index_Array
Matlab Commands;
end
1 | for j=1:3:200 |
check if the number is even
1 | for i = 1:10 |
Calculate sum that can be divided by 3 in first 10 numbers
1 | s = 0; |
while loop
while (condition)
Matlab Commands;
end
1 | while ((a>3) & (b==5)) |
Function
After write this function, run the program, MATLAB will ask you to input. Rember the function name must match the file name.
1 | function out = squarer2(A, ind) |
check if a number is even, if even returns 1
1 | function y = is_even(x) |
strcmp, strfind, fgetl
tf = strcmp(s1,s2) compares s1 and s2 and returns 1 (true) if the two are identical. Otherwise, strcmp returns 0 (false). Text is considered identical if the size and content of each are the same. The return result, tf, is of data type logical.
k = strfind(str,pattern) searches str for occurrences of pattern. The output, k, indicates the starting index of each occurrence of pattern in str. If pattern is not found, then strfind returns an empty array, []. The strfind function executes a case-sensitive search.
tline = fgetl(fileID) returns the next line of the specified file, removing the newline characters. fileID is an integer file identifier obtained from fopen. tline is a text string unless the line contains only the end-of-file marker. In this case, tline is the numeric value -1.
Struct
1 | my_struct.name = 'My new struct' |
my_struct =
name: ‘My new struct’
ans =
name: ‘My new struct’
age: 25
gender: ‘f’
my_struct.contact
ans =
phone: 123456
email: ‘person@gmail.com’
Altenative way
1 | S = struct('name', 'Bob', 'email', 'bob@gmail.com') |
S =
name: ‘Bob’
email: ‘bob@gmail.com’
1 | my_cell{1} = 'hello world' --> call mycell, it simply print hello world |
Plot Data
Plot Fibonacci
1 | y = [1,1,2,3,5,8,13,21] |
Plot sin and cos
1 | x = linspace(0, 2*pi, 100); |
Add noise point
1 | x = linspace(0, 2*pi, 1000); |
- scatter is sorted, helpful in data analysis
bar chart
1 | x = 1:10 |
Histogram
1 | x = randn(1000,1); |
Pie chart
1 | x = 1:5; |
File I/O
1 | csvread() |
subplots, 3D plot
1 | x = linspace(0, 2*pi, 100); |
3D plot
1 | z = randn(100,100); |
Title and label
1 | title('A plot of sin(x)') |
Complete code to generate two graphs for sinx and cos x
1 | x = linspace(0, 2*pi, 100); |
Sound processing
1 | d = audioread('daliwan.wav') |
Reverse audio
1 | d2 = flipud(d); |
2x speed
1 | d4 = downsampel(d,2) --> d was the original audio |
Generate random values
1 | randi(10) |
ans =
9
1 | R = randi(10, 3) |
R =
10 7 6
2 1 10
10 3 10
Histograph with random values
1 | hist(rand(1000,1) |