Syntax
MATLAB’s syntax is a small, idiosyncratic blend of Fortran, Algol, and the array-language tradition (APL, IDL). Statements are written one per line; the line is the natural terminator. Semicolons separate statements on a single line and suppress the printing of the resulting expression — a dual role that surprises newcomers. Comments begin with %; the longer %% opens a section that the editor treats as a unit. The combination — line-oriented, no block-delimiter braces, keyword-terminated control structures (if ... end), the display-by-default rule, and the matrix-construction syntax — gives MATLAB code a distinctive look.
A complete program
function y = trapezoid(f, a, b, n)
% TRAPEZOID Approximate the integral of f from a to b using n trapezoids.
h = (b - a) / n;
x = linspace(a, b, n + 1);
y = h * (sum(f(x)) - (f(a) + f(b)) / 2);
end
A separate script — demo.m:
g = @(x) sin(x) ./ (1 + x.^2);
I = trapezoid(g, 0, pi, 1000);
fprintf("I ≈ %.6f\n", I)
The principal features visible:
function y = trapezoid(f, a, b, n)— a function file with one outputy.% TRAPEZOID Approximate ...— a comment; the first contiguous comment block following the function declaration is the H1 line and help text, returned byhelp trapezoid.linspace(a, b, n + 1)— standard-library function returning a row vector.sum(f(x))—f(x)is broadcast over the vectorx;sumreduces along the first non-singleton dimension.g = @(x) sin(x) ./ (1 + x.^2)— an anonymous function. The./and.^are element-wise operators.end— keyword terminator. Required for functions in scripts and recommended for all functions.
Statements and the semicolon rule
A statement is an assignment, a function call, or an expression. The line is the natural terminator. Statements may be put on a single line separated by commas (display the result) or semicolons (suppress the result):
a = 1, b = 2; c = 3
% a = 1
% c = 3
% (b is set but not displayed)
The semicolon’s role as a display suppressor is the language’s most-cited surprise. The convention in scripts is to terminate almost every statement with a semicolon, with deliberate exceptions for diagnostic output. The display rule is also what produces the ans variable: an expression with no assignment binds the value to ans and displays it.
1 + 1
% ans = 2
ans * 3
% ans = 6
Continuation is by ... at the end of a line; the continuation does not span a comment:
total = a + b + c + ...
d + e + f;
Comments
x = 1; % single-line comment
y = 2; % comments to end-of-line
%{
A block comment. The opening %{ and closing %} must each be on
their own line. Block comments do not nest.
%}
%% Section header
% A line beginning with %% is a section marker; the editor's "Run Section"
% command executes from this marker to the next.
The %% section marker is part of the editor’s interactive workflow and has no effect on batch execution; it is documented here because the convention is widespread.
Keywords and identifiers
MATLAB reserves a small set of keywords:
break,case,catch,classdef,continue,else,elseif,end,for,function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while
The list is small because MATLAB’s control structures are the only block-introducing constructs; everything else is a function. The keyword end terminates if, for, while, switch, try, function, and classdef; it also has a second meaning inside an index expression, where it refers to the last index of the dimension being indexed. Context disambiguates.
Identifiers are case-sensitive ASCII letters, digits, and underscores; the first character must be a letter. The maximum length is namelengthmax — 63 since R2007a. Conventional names use camelCase for variables and functions and PascalCase for classes, though no convention is enforced and the standard library mixes styles (linspace, fzero, set, gca).
The principal namespace-related surprise is that functions and variables share a namespace: a name resolves first to a workspace variable, then to a function on the path. Assigning sum = 5 shadows the sum function until sum is cleared with clear sum. The lint warning “sum might be a variable” is the diagnostic.
Matrix construction and the comma-newline rule
Matrices are constructed with square brackets. Elements on a row are separated by commas or whitespace; rows are separated by semicolons or newlines:
A = [1 2 3
4 5 6
7 8 9];
B = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % identical to A
The two forms are exactly equivalent. The newline-as-row-separator inside brackets is the reason long matrix literals can be laid out two-dimensionally — a convenience that array-first languages share. The same rule applies to cell arrays with {} and to row vectors of strings.
A matrix can be built from sub-matrices of compatible shape:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [A B]; % horizontal concatenation: 2-by-4
D = [A; B]; % vertical concatenation: 4-by-2
The colon operator
The colon is the most overloaded operator in the language. As a range constructor it produces a row vector:
1:5 % [1 2 3 4 5]
0:0.25:1 % [0 0.25 0.5 0.75 1]
10:-2:0 % [10 8 6 4 2 0]
As an index, a lone colon selects the entire dimension:
A(:, 1) % the first column
A(2, :) % the second row
A(:) % the matrix flattened to a column vector
The colon’s two roles are unrelated; the reader learns to disambiguate by context.
Function-call syntax
A function call uses parentheses and a comma-separated argument list. The same syntax indexes an array:
sin(pi/4)
A(2, 3) % element of A at row 2, column 3
The reader has no way to tell from a fragment of source which is which without knowing whether sin and A are variables or functions. The MATLAB parser resolves the question at the moment of evaluation: it consults the workspace and the path. This is the principal source of the parenthesis surprise, and it shapes the design of every page that follows.
A function may have multiple outputs, requested with bracketed left-hand side:
[Q, R] = qr(A); % QR factorisation
[V, D] = eig(A); % eigenvectors and eigenvalues
[m, idx] = max(x); % maximum and its index
A function that produces outputs the caller does not ask for simply does not produce them; nargout inside the function reveals how many outputs were requested.
Command-form syntax
For convenience, MATLAB accepts a command form that omits parentheses and treats the arguments as unquoted strings:
clear all % equivalent to clear('all')
format short
help sin
disp hello % equivalent to disp('hello')
The form is permitted for any function whose entire argument list is a sequence of bare-word tokens; the parser converts each token to a string. The convenience is real (one less character per command in the REPL); the surprise is that disp 1+1 displays the string '1+1', not the number 2. The recommendation in code that is not interactive is to use the parenthesised form.