Quick Matlab
Posted on September 2, 2022
Tags: mathematica
1 Basics
1.1 Variables
Semicolon suppress output
a = 5;
b = 9;
% Saving variables to file
save datafile.mat %save all variable to file
save datafile.mat a %save variable a to file
clear %clear variables
% Loading variable from file
load datafile.mat %load all variable from file
load datafile.mat a %load variable a from file
1.2 Take number input
mynum = input("put your number here:");
doublednum = mynum * 2
1.3 Take string/filepath input and show image
filepath = input("","s");
img = imread(filepath);
imshow(img);
imgR = imresize(img,0.5);
I = rgb2gray(imgR);
imshow(I)
1.4 Build your own function
- Also necessary for code gen
- Right-Click in folder >> New >> Script
- Right-Click in folder >> New >> Function
In matlab you dont need to compile the function file. You can just run the main script that calls the function.
xaxis = 1:12
yaxis = rand(1,12)
plot(xaxis,yaxis)
hold on %plots both graph on top instead of separate plots
plot(xaxis,yaxis+0.1,"r--o") %r for red, -- with o as marker
hold off
plot(yaxis) %x-axis auto ranges from 1 to size of yaxis
imshow(filtr("DPlayer.png"))
2 Image label and training
Matlab use Simulink image labeler export will output: gTruth
trainingDataTable = objectDetectorTrainingData(gTruth)
acfDetector = trainACFObjectDetector(trainingDataTable,'NegativeSamplesFactor',2);
I = imread("C:\Users\User\Downloads\archive\shapes\squares\drawing(28).png");
bboxes = detect(acfDetector,I);
annotation = acfDetector.ModelName;
I = insertObjectAnnotation(I,'rectangle',bboxes,annotation);
figure
imshow(I)
2.1 Common math functions
%constants and functions
pi_ = pi
sin_ = sin(90)
eig_ = eig(ones(5))
abs_ = abs(-5)
imaginaryNum = 1i^2
2.2 Matrices and Vectors
%row vector
x1 = [3, 5, 7]
x2 = [3 5 7] %we can use comma or space
%col vector
y = [3;5;7]
z = [1 2 3;4 5 6] %2 by 3 matrix
%select element in matrix
z(2,3) % element in position 2,3
z(:,3) % allrow,third column
2.2.1 Transpose
%Transpose using single quote '
xT = [3 5 7]'
2.3 Matrix Operations
test1 = [3 5 7 12 10] %we can use comma or space
test1(1) %output 3, first element
test1(1) = 9 %change first element
test1(1:2) = [4 2] %change first 2 elements
test1([1 3]) %output 1st and 3rd element
test1(end) %last element
test1(end-1) %next to last element
test1 = [3 5 7 12 10]
%logical indexing
test1 > 3 %outputs a mask
test1(test1 > 3)
test1(test1 == 3)
test1(test1 ~= 3)
test1(test1 > 3 & test1 < 7)
test1(test1 > 3 | test1 < 7)
test1(test1 ~= 3) = 99 %replace elem neq 3 with 99
2.4 Plotting
xaxis = 1:12
yaxis = rand(1,12)
plot(xaxis,yaxis)
hold on %plots both graph on top instead of separate plots
plot(xaxis,yaxis+0.1,"r--o") %r for red, -- with o as marker
hold off
plot(yaxis) %x-axis auto ranges from 1 to size of yaxis
3 Image label and training
Matlab use Simulink image labeler export will output: gTruth
trainingDataTable = objectDetectorTrainingData(gTruth)
acfDetector = trainACFObjectDetector(trainingDataTable,'NegativeSamplesFactor',2);
I = imread("C:\Users\User\Downloads\archive\shapes\squares\drawing(28).png");
bboxes = detect(acfDetector,I);
annotation = acfDetector.ModelName;
I = insertObjectAnnotation(I,'rectangle',bboxes,annotation);
figure
imshow(I)
4 Code generation
- Right-Click in folder >> New >> Script
- Right-Click in folder >> New >> Function
Code gen must be functions. But we can run any process inside the function file so we use the Script file to test the function.
function [myOutput] = filtr(myInput)
%FILTRFUNCTION Summary of this function goes here
% Detailed explanation goes here
myOutput = eig(myInput)
end
filtr(rand(3,3))
Go to Apps >> Matlab Coder.
- Selection function file path,
filtrFunction.m
NOT our scriptmyscript.m
- Defaults for entry point function
- It will prompt
To automatically define input types, call filtr or enter a script that calls filtr in the MATLAB prompt below:
Here we select the script that CALLS our function which ismyscript.m
. Then click Autodefine Input Types. - Check for runtype issues by running the compiled function
filtrFunction.mex
on our caller scriptmyscript.m
- Build as C,C++, or mex