← Back to Home
📊
Programming Languages
MATLAB
MATLAB commands, functions, and syntax
💻 Basic Commands
| Command | Description |
|---|---|
clc | Clear command window |
clear | Clear all variables from workspace |
clear x | Clear specific variable |
close all | Close all figure windows |
who / whos | List variables / with details |
help func | Get help for function |
doc func | Open documentation |
pwd | Current directory |
cd path | Change directory |
ls / dir | List files |
🔢 Matrix Operations
| Command | Description |
|---|---|
A = [1 2; 3 4] | Create 2x2 matrix |
zeros(m, n) | Matrix of zeros |
ones(m, n) | Matrix of ones |
eye(n) | Identity matrix |
rand(m, n) | Random matrix (uniform) |
randn(m, n) | Random matrix (normal) |
linspace(a, b, n) | n evenly spaced points |
a:step:b | Range with step |
A' | Transpose |
A * B | Matrix multiplication |
A .* B | Element-wise multiplication |
A ./ B | Element-wise division |
A .^ n | Element-wise power |
inv(A) | Matrix inverse |
det(A) | Determinant |
eig(A) | Eigenvalues |
rank(A) | Matrix rank |
📍 Indexing & Slicing
| Command | Description |
|---|---|
A(i, j) | Element at row i, column j |
A(i, :) | Entire row i |
A(:, j) | Entire column j |
A(end, :) | Last row |
A(1:3, :) | First 3 rows |
A(:) | All elements as column vector |
size(A) | Dimensions of matrix |
length(A) | Largest dimension |
numel(A) | Number of elements |
📐 Math Functions
| Command | Description |
|---|---|
sum(A) | Sum of elements |
mean(A) | Average |
std(A) | Standard deviation |
max(A) / min(A) | Maximum / minimum |
abs(x) | Absolute value |
sqrt(x) | Square root |
exp(x) | Exponential |
log(x) / log10(x) | Natural / base-10 log |
sin(x) / cos(x) / tan(x) | Trigonometric functions |
floor(x) / ceil(x) | Round down / up |
round(x) | Round to nearest |
mod(a, b) | Modulo |
📈 Plotting
| Command | Description |
|---|---|
plot(x, y) | Basic 2D line plot |
scatter(x, y) | Scatter plot |
bar(x, y) | Bar chart |
histogram(data) | Histogram |
subplot(m, n, p) | Create subplot grid |
hold on / off | Keep/clear current plot |
title('text') | Add title |
xlabel('text') | X-axis label |
ylabel('text') | Y-axis label |
legend('a', 'b') | Add legend |
grid on / off | Toggle grid |
axis([xmin xmax ymin ymax]) | Set axis limits |
figure | New figure window |
saveas(gcf, 'file.png') | Save figure |
surf(X, Y, Z) | 3D surface plot |
mesh(X, Y, Z) | 3D mesh plot |
contour(X, Y, Z) | Contour plot |
⚙️ Programming
| Command | Description |
|---|---|
if condition ... end | Conditional statement |
for i = 1:n ... end | For loop |
while condition ... end | While loop |
function y = f(x) | Define function |
break | Exit loop |
continue | Skip to next iteration |
return | Return from function |
try ... catch ... end | Error handling |
% comment | Single-line comment |
%{ ... %} | Block comment |
⌨️ Editor Shortcuts
| Shortcut | Description |
|---|---|
| F5 | Run script |
| F9 | Run selected code |
| Ctrl + C | Interrupt execution |
| Ctrl + Enter | Run current section |
| Ctrl + R | Comment selection |
| Ctrl + T | Uncomment selection |
| Ctrl + I | Smart indent |
| Ctrl + D | Open selected function |
| Tab | Autocomplete |
| Up Arrow | Previous command |
📝 Common Patterns
% Read data from file
data = readmatrix('file.csv');
% Anonymous function
f = @(x) x.^2 + 2*x + 1;
% Find indices where condition is true
idx = find(A > 5);
% Apply function to each element
result = arrayfun(@(x) x^2, A);
% Solve linear system Ax = b
x = A \ b;