Introduction, Variables, Functions, and Image Processing Basics
MATLAB is a high-level programming language and environment used for numerical computation, data analysis, visualization, and algorithm development. It is widely used in engineering, physics, and computer science.
Create variables easily; MATLAB automatically handles types.
x = 10; y = 5; z = x + y; disp(z) % displays 15 A = [1 2 3; 4 5 6]; % a 2x3 matrix B = A * 2; % multiply each element by 2 disp(B)
Visualize data quickly using built-in plotting functions.
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y)
xlabel('x')
ylabel('sin(x)')
title('Simple Sine Wave')
grid on
MATLAB has powerful tools for reading, displaying, and manipulating images.
img = imread('peppers.png'); % read an image
imshow(img); % display image
grayImg = rgb2gray(img); % convert to grayscale
imshow(grayImg)
bwImg = imbinarize(grayImg); % convert to black & white
imshow(bwImg)
imread, imshow, and rgb2gray are part of MATLAB's **Image Processing Toolbox**.
Functions allow you to reuse code efficiently.
function y = squareNum(x)
y = x^2;
end
result = squareNum(5);
disp(result) % prints 25
MATLAB comes with many built-in libraries and toolboxes.
% Example: Using Image Processing Toolbox
img = imread('peppers.png');
edges = edge(rgb2gray(img), 'Canny'); % detect edges
imshow(edges)