Matlab Scripts and live Scripts
Commands:
Change the path: create a folder, copy the windows path of the forlder and paste it in the matlab
explorer to get into that folder, Some alternative commands for above instructions are: >>cd ‘path’
>>pwd >>ls
To display results and clear: >>disp(variable), >>clear, >>clc, >>close all
Right click on the command history to create Scripts and save it as .m and to run it play it
And you can hit left pane to breakpoint to step by step run of the program and Quit it to close the
debug. To end the debug >>dbquit
Comment is % and semi colon is used to stop giving in line variables’ measures.
If Script is saved as .mlx then it is live Scripts and the benefit is the result would show in the script itself
as you are coding without asking for run. Live Script is used for Presentation and lectures. You can
import image and headings as a points of the lectures and presentation and no require for comments.
By the way you still have to click PLAY to see the results in line in the live scripts.
To read the image from the root directory, the command is >>imread(‘.tif’); and then need that to show
as a result >>imshow(variablecontainingimageread)
a=[1 2 9 4 5]
disp(a)
b=length(a)
c=a(3) to put 3rd element
a(3)=10 to change the value of a in the 3rd element from 9 to 10
a=1:10
a=1:2:10 for incremental value
a=[1 2 3; 5 6 7; 8 9 10]
b=a’ for transpose
c=max(max(a)) is 9
c=min(min(a)) is 1
[e var]=eig(a) for eigen value of the eigen vector
a=a.*2 for matrix multiplication
a=a./2
>>whos to find the variable types
Cell Arrays and Structures:
CellArrays.mlx that means at each element place there can be different type of variavle
Clear; clc; a{1,1}=6; a{1,2}=’String’; a{1,3}=1:10; a{1,4}=[1 2 3;4 5 6; 7 8 9]; disp(a)
Now to put value in this cell, and you can know it by whos
Now if b=a{1,3} so b will be 1 2 3 4 5 6 7 8 9 10 that is injecting the a ‘s 3 rd element into b
and to replace 5 with 30 then >>b(5)=30; then 1 2 3 4 30 6 7 8 9 10
>>a{1,3}=b; now you can injecting b’s value into a’s 3 rd element.
Loops and Functions:
function[outputArg1, outputArg2] = example(inputArg1, inputArg2)
outputArg1 = inputArg1;
outputArg2 = inputArg2;
end
here function name example should be same as the file name and the result would be shown in the
function output but you can still get the result if you ask for disp(result);
this function can be called from the main program named as .m
now error comes if there are two output arguments in the function but in your main you arranged the
prog
ram in a way that it is looking for one result. It does not matter what name is given in the output
argument but its important to know how many arguments are coming out.
a=5; a=5; a=5; a=input(‘input the no. -->’);
for i=1:10 while a<100 while(1) infinite while loop if a>5
for j=1:10 a=a+1; a=a+1; disp(‘a is greater’);
a=a+1; end if a>50 elseif a==5
end disp(a) break disp(‘a is 5’)
end end else
end disp(‘a is smaller’);
disp(a) end
You can use many functions in a file but in that case one is main function which should be the name of
the file and other functions are to be called so if you are calling those functions then those functions
need to be finished with return and not with end. And this main function does not end with end
Switch case: character value Switch case: numerical value
variable = input(‘enter the character’, ‘s’); s is char variable = input(‘enter the number’);
switch lower(variable) switch (variable)
case {‘linear’, ‘bilinear’} case 1
disp(‘method is linear’) disp(‘method is linear’)
case ‘cubic’ case 2
disp(‘method is cubic’) disp(‘method is cubic’)
case ‘nearest’ case 3
disp(‘method is nearest’) disp(‘method is nearest’)
otherwise otherwise
disp(‘method is unknown’) disp(‘method is unknown’)
end end
>>diary(‘myplots.m’); and after this command
whatever command is put as command line will
be recorded/stored inside myplots.m file, the file
will also be created now. >>diary(‘mysubplots.m’)
>>a=linspace(0,6); for linear array >>a=linspace(0,6); for linear array
>>b=sin(a); >>b=sin(a);
>>plot(a,b); >>subplot(2,2,1);plot(a,b);
>>h=figure(1); >> subplot(2,2,2);plot(a,b,’r-’);
>>plot(a,b,’r--’); r for red and – for line types. >> subplot(2,2,3);plot(a,b,’r--’);
>>c=cos(b);
>>plot(a,c,’r--’, ‘LIneWidth’, 3); >>c=cos(b);
>>hold on; it means lastly shown plot will be >> subplot(2,2,4);plot(a,c,’r--’, ‘LineWidth’, 3);
merged with the plot that is plotted next
>>plot(a,b,‘r--’);
>>grid on;
>>xlabel(‘X-Axis’);
>>ylabel(‘Y-Axis’);
>>title(‘Plot’);
>>legend(‘sin’, ‘cos’);
>>gtext(‘place text anywhere’);
>>diary off
>>h=figure(1);
>>saveas(h, ‘plot.png’);
close all; to close the hold on and all other previous plots if you are trying to make a different plot I the
program next to it.
For bargraph.m
For one bar For multi bar
clc; to clean previously plotted graphs if any clc; to clean previously plotted graphs if any
clear; for previous graphs to clean if any before clear; for previous graphs to clean if any before
a=[10 20 30 40]; a=[10, 29; 20, 40; 30, 40; 40, 60];
bar(a, ‘r’); bar(a);
xticks(1:4); xticks(1:4); if you want to make it horizontal then
xticklabels({’10-20’, ’20-30’, ’30-40’, ’40-50’}); just change to y
xticklabels({’10-20’, ’20-30’, ’30-40’, ’40-50’}); if
horizontal just y
legend(‘Before Training’, ‘After training’);
xlabel(‘Age Group’); if horizontal just y
ylabel(‘Performance’); if horizontal just x
For pie graphs
clc;
clear;
a=[10, 20, 30, 40];
pie(a);
title(‘Market Share’);
legend(‘A’, ‘B’, ‘C’, ‘D’);
Saving and loading a variable with load:
>>clear>>a=5;>>save a a; saving variable a in a but in case you want to save all variables before save
command then just save it as a that is if >>a=5;>>b=6;>>c=7; and then >>save a then all a b c variables
will be saved in a(here a is not variable but the load command’s variable).
>>clear
>>load a
To read message inside word To write message inside word
In .mlx In .mlx
a=fopen(‘message.txt’); where it is written hello a=fopen(‘message.txt’, ‘w’); hello with String
b=fread(a); b=’String’
b=char(b)’; fwrite(a,b);
disp(b); fclose(a);
fclose(a);
To read message inside excel To write message inside excel
[ndata, text, alldata]=xlsread(‘test.xls’);
disp(alldata)
So the following points are covered in here:
- Variables, Arrays and Matrices
- Structures and Cell Arrays
- Loops and Functions(for, ifm while, switch case, function and live function)
- Plots and Subplots, Bar, Pie
- Import and Export various file formats(text, excel, load and save)
Image Processing using Matlab
How to read and store image, view pixel
a=imread(‘cameraman.tif’); read pixel in image
and put it in variable a
[r c p] = size(a); it will give you the size of image
imshow(a); to display the image
whos a to see the size of image
impixelinfo; value of pixel as you move you cursor
on the image.
imwrite(a,’test.png’);a to png format
%Medical Image
info=dicominfo(‘CT-MONO2-16-ankle.dcm’); this
is the medical image .dcm and here we are
reading this medical image and it has 16 bit data
where has imread can read 8 bit data
Y=dicomread(info);
figure, imshow(Y,[]);
Types of images
Mathematical operations in image
Channel separation
Edge detection
Noise and filtering
Thresholding and image segmentation
Video Processing using Matlab
Medial Image Processing using Matlab
Graphical User Interface Matlab
App Development using Matlab
Computer Vision using Matlab
Fuzzy Logic Design using Matlab
Neural Network using Matlab
Machine Learning using Matlab
Deep Learning using Matlab
Neuro Fuzzy Designer using Matlab
Image Segmentation using Matlab
Image Compression using Matlab
Feature Extraction using Matlab
Face Recognition using Matlab
Augmented Reality using Matlab
Image Denoising using Matlab
Arduino Programming using Matlab
Image Quality Metrics using Matlab
Data Hiding using Matlab(Steganography)
Real time Object Detection using Matlab
Raspberry Pi Programming using Matlab
Speech Processing using Matlab
Audio Processing using Matlab
Jetson Nano Programming using Matlab
Cryptography using Matlab
Machine Learning and IoT using Matlab
SLAM using Matlab
Semantic Segmentation using Deep Learning Matlab