Introduction to MATLAB & Setup
What is MATLAB?
MATLAB (MATrix LABoratory) is a high-level programming language and interactive environment developed by MathWorks for numerical computing, data analysis, algorithm development, and visualization. It is widely used in engineering, science, and academia.
Why MATLAB?
- Built-in matrix and array operations
- Outstanding visualization tools
- Rich ecosystem of toolboxes
- Industry standard in engineering and research
Installation & Setup
Download MATLAB from mathworks.com. Use the online version (MATLAB Online) if you prefer no installation.
>> disp('Hello, MATLAB Mastery!')
Hello, MATLAB Mastery!
>> version
ans =
'9.14.0.XXXXXX (R2023a)'
Open MATLAB, run the command ver to see your version, and create a simple script
that prints your name and today's date using datestr(now).
Variables & Matrices
Matrices: The Core of MATLAB
In MATLAB, everything is a matrix. A single number is a 1x1 matrix.
% Row vector r = [1 2 3]; % Column vector c = [1; 2; 3]; % 2x2 Matrix M = [1 2; 3 4];
Creating Special Matrices
zeros(3,3): 3x3 matrix of zerosones(2,4): 2x4 matrix of oneseye(3): 3x3 Identity matrix
Operators & Expressions
Element-wise vs Matrix Operations
This is a critical concept in MATLAB. Use a dot . for element-wise operations.
A = [1 2; 3 4]; B = [2 2; 2 2]; C1 = A * B; % Matrix multiplication C2 = A .* B; % Element-wise multiplication ([1*2 2*2; 3*2 4*2])
Control Flow
Conditional Logic
if x > 0 disp('Positive') elseif x < 0 disp('Negative') else disp('Zero') end
Scripts & Functions
Creating a Function
function area = calcCircleArea(r) area = pi * r^2; end
Arrays & Matrix Operations
Linear Algebra
A = [1 2; 3 4]; inv(A) % Inverse det(A) % Determinant eig(A) % Eigenvalues
Plotting & Visualization
2D Plotting
x = 0:0.1:10; y = sin(x); plot(x, y, 'r--', 'LineWidth', 2); title('Sine Wave'); xlabel('Time'); ylabel('Amplitude'); grid on;
Data Import & Export
Reading Data
T = readtable('data.csv');
data = T.Variables;
Symbolic Math
Solving Equations
syms x eqn = x^2 + 5*x + 6 == 0; S = solve(eqn, x);
Programming Structures
Cell Arrays & Structs
C = {1, 'text', magic(3)};
S.name = 'Project';
S.value = 100;
Simulink & App Designer
Introduction to graphical programming and building interactive apps within MATLAB.
Capstone: Engineering Analysis
Process a large dataset of sensor readings, filter noise, and generate a multi-panel visualization report.
% Project steps: % 1. Import raw sensor data % 2. Apply a moving average filter % 3. Compute statistical metrics % 4. Export findings to a PDF report