MATLAB Cheat Sheet
Basics
// variables, output, help
x = 5; y = [1 2 3]; z = [1; 2; 3]; s = "text"; c = 'chars';
disp(x); fprintf('%d items, %.2f avg\n', n, avg); num2str(3.14); str2double('2.5')
help plot; doc plot; clc; clear; close all; whos
Matrices
A = [1 2; 3 4]; A'; A * B; A .* B; A \ b; inv(A); det(A); eig(A)
zeros(2, 3); ones(3); eye(3); rand(2); randi(10, 1, 5); magic(4); linspace(0, 1, 5)
size(A); numel(A); length(v); reshape(v, 2, 3); [A, B]; [A; B]
A(2, :); A(:, end); v(v > 2); find(v == 3); sum(A); mean(A, 2); max(v); sort(v)
Control flow and functions
if x > 0, disp('pos'); elseif x == 0, disp('zero'); else, disp('neg'); end
for i = 1:10, end; while cond, end
switch s, case "a", disp(1); otherwise, disp(0); end
f = @(x) x.^2 + 1; f(3)
function out = myfun(a, b), out = a + b; end
Data and files
st.name = "A"; st.marks = [1 2]; c = {1, 'a'}; c{2}
T = readtable('data.csv'); T.Marks; writetable(T, 'out.csv')
M = readmatrix('m.csv'); writematrix(M, 'm.csv'); save('ws.mat'); load('ws.mat')
Plotting and numerics
plot(x, y, 'r--'); hold on; xlabel('x'); ylabel('y'); title('t'); legend('a'); grid on
subplot(2, 1, 1); bar(v); histogram(v); scatter(x, y); surf(X, Y, Z)
roots([1 -3 2]); polyfit(x, y, 1); polyval(p, x); integral(f, 0, 1); fzero(f, 1)
[t, y] = ode45(@(t, y) -y, [0 5], 1);
Full MATLAB documentation