MATLAB
A high-level numerical computing environment and programming language built around the matrix as the fundamental datum. Pervasive in engineering, scientific computing, control systems, signal and image processing, and finance; sold as a commercial product by The MathWorks since 1984.
- Paradigms
- imperative · array · OOP · functional
- Typing
- dynamic
- Version
- R2024b
- First released
- 1984
MATLAB — short for MATrix LABoratory — is a high-level programming language and interactive numerical-computing environment in which the matrix is the fundamental datum and almost every value is, in some sense, an array. A scalar in MATLAB is a one-by-one matrix; a vector is a one-by-N or N-by-one matrix; a string was historically a one-by-N matrix of characters and remains so unless the newer string class is invoked. Arithmetic operators are linear-algebraic by default — A*B is the matrix product and A/B solves a linear system — and element-wise counterparts are written with a leading dot (A.*B, A./B). The combination of array-first semantics, an interactive REPL, a large library of numerical algorithms, and integrated graphics has made MATLAB the de-facto language of engineering education and of much practical engineering work for four decades.
MATLAB is a commercial product, sold and developed by The MathWorks, Inc. of Natick, Massachusetts. The base product is augmented by approximately a hundred toolboxes — separately licensed libraries that extend the language with domain-specific functions for signal processing, image processing, control-system design, statistics and machine learning, optimisation, symbolic mathematics, deep learning, and so on. The accompanying graphical environment, Simulink, provides block-diagram modelling for dynamic systems and code-generation to C, HDL, and PLC code; many MATLAB engagements in industry are in fact Simulink engagements.
History
MATLAB was created in the late 1970s by Cleve Moler, then a mathematics professor at the University of New Mexico, to give his linear-algebra students convenient access to the LINPACK and EISPACK Fortran libraries without having to write Fortran. The original program was a small interpreter, written in Fortran, that accepted a few dozen commands and dispatched them to the library. It was a teaching tool, distributed freely.
In 1984, Moler and Jack Little — a Stanford-trained control engineer who had recognised the program’s commercial potential — rewrote the interpreter in C, founded The MathWorks, and shipped MATLAB 1.0 as a commercial product for the IBM PC. The early customers were control engineers, whose work involves frequent manipulation of matrices arising from linear-time-invariant system models, and the early extensions — the Control System Toolbox, the Signal Processing Toolbox, the Optimization Toolbox — reflected that constituency.
The interpreter was rewritten for object-oriented graphics in MATLAB 4 (1992), gained an integrated development environment in MATLAB 5 (1996), added structures and cell arrays in the same release, and reworked its object system substantially in MATLAB 7 (2004). The modern, MATLAB-Class-Definition (MCOS) object system — written as classdef files with explicit value and handle semantics — arrived in R2008a (2008) and superseded the older @-folder class mechanism. The JIT (just-in-time compilation) facility, introduced in R13 (2002), substantially closed the performance gap between vectorised and explicitly looped MATLAB code, although vectorisation remains the idiomatic recommendation.
Recent releases (named by year and half: R2024a, R2024b) have introduced string arrays (R2016b), tall arrays for out-of-core computation (R2016b), gpuArray and Parallel Computing Toolbox GPU support, live scripts (R2016a), function arguments validation blocks (R2019b), the modern dictionary type (R2022b), and an evolving suite of deep-learning and generative-AI tooling.
Hello world
A complete MATLAB program in a file hello.m:
disp("Hello, world!")
Run from the command line:
matlab -batch "hello"
A slightly more representative example — solving a 3×3 linear system and plotting a sine wave — illustrates the language’s character:
A = [ 2 1 -1
-3 -1 2
-2 1 2];
b = [ 8; -11; -3];
x = A \ b; % solve Ax = b by Gaussian elimination
disp(x) % [ 2; 3; -1 ]
t = linspace(0, 2*pi, 200);
y = sin(t);
plot(t, y, "LineWidth", 1.5)
title("y = sin(t)")
The features visible in those few lines: the matrix literal with rows separated by newlines (or by semicolons), the backslash operator \ that solves a linear system, the linspace constructor returning a row vector, the element-wise sin that broadcasts over the input, and the plot function whose argument list mixes positional data and trailing name-value pairs.
The language’s character
A working programmer encounters several distinctive features:
- One-based indexing.
A(1)is the first element. The notationendinside an index refers to the last index:A(end),A(end-1:end). - Indexing and function call share syntax.
A(2)could be either an array element or a function call depending on whetherAis a variable or a function in the current scope. This conflation is the source of MATLAB’s most-cited surprise and is the reason functions and variables share a namespace. - The matrix is everywhere. A scalar is a 1×1 matrix. The arithmetic operators are linear-algebraic by default; element-wise versions are written with a leading dot. The transpose operator is
'; its non-conjugating cousin is.'. - Vectorisation. Idiomatic MATLAB avoids loops in favour of vectorised expressions:
sum(x.^2)instead of aforloop over the elements. The JIT has narrowed the performance gap, but vectorised code remains both faster and clearer in most cases. - Function files. A
.mfile that begins with thefunctionkeyword is a function; otherwise it is a script. A file may contain local functions after the principal function and, since R2016b, may contain functions directly in a script. - The workspace. MATLAB’s REPL maintains a workspace — a flat namespace of variable bindings — that survives between commands. The
whoscommand lists its contents. Workspace state is the basis of the language’s interactive feel. - Plotting and figures. Plotting is part of the language:
plot,surf,imagesc,histogram. Figures are top-level objects with handles and properties; the convention is to set properties through name-value pairs (plot(x, y, "LineWidth", 1.5, "Color", "red")). - No semicolon means show. A statement terminated by a semicolon executes silently; without a semicolon the resulting value is displayed. This is unusual among modern languages and is the cause of much accidentally noisy output.
Toolboxes and Simulink
A bare MATLAB installation is sometimes called base MATLAB; the language is augmented by toolboxes and by the Simulink simulation environment. Toolboxes are libraries of MATLAB functions (and increasingly Java- and C-implemented routines exposed through the MEX interface) that extend the language for particular domains.
| Toolbox (selected) | Provides |
|---|---|
| Statistics and Machine Learning | Distributions, hypothesis tests, classification, regression, clustering. |
| Signal Processing | FIR/IIR design, spectral estimation, filter banks. |
| Image Processing | Morphology, segmentation, registration, file-format support. |
| Control System | LTI models, root locus, Bode and Nyquist plots, PID tuning. |
| Optimization / Global Optimization | Linear, quadratic, nonlinear, integer, and global solvers. |
| Symbolic Math | A computer-algebra system (built on MuPAD / now an in-house engine). |
| Deep Learning | dlnetwork, training loops, transfer learning, ONNX import/export. |
| Parallel Computing | parfor, parfeval, spmd, GPU and cluster execution. |
Simulink is a separate but tightly integrated graphical modelling environment in which dynamic systems are described as block diagrams and simulated, fixed-step or variable-step, by numerical integrators. Simulink models can call MATLAB code and vice versa; from a model, Embedded Coder and HDL Coder can generate production C, C++, HDL (VHDL/Verilog), or PLC code for deployment on target hardware. Much MATLAB work in industry — particularly in automotive, aerospace, and energy — is in fact Simulink work.
Code generation, MEX, and external interfaces
MATLAB is a language without a free, open-source implementation produced by The MathWorks; the closest free alternative is GNU Octave, which is a separate project with substantial but incomplete syntactic and semantic compatibility. The official MATLAB product offers several ways to bridge to other systems. MEX files are dynamically linked libraries written in C, C++, or Fortran that expose functions callable from MATLAB; they use a defined API (mexFunction, mxArray) and have been the canonical mechanism for performance-critical extensions for thirty years. MATLAB Coder generates portable C and C++ source from a subset of MATLAB; MATLAB Compiler packages MATLAB programs as standalone executables or shared libraries that run under a free runtime. The language interoperates with Python (pyrun, py.module.function(...)), Java, .NET, and C++ (via MATLAB Engine API for C++); and from Python, the MATLAB Engine API for Python permits MATLAB to be embedded as a library in a Python program.
A note on this volume
The pages that follow document the language as a working programmer encounters it. Where MATLAB has features without close analogue in general-purpose languages — implicit expansion, the indexing/call ambiguity, the value-vs-handle class distinction, the workspace model, the function-vs-script file distinction — the pages dwell on them. Where MATLAB shares features with other languages — if/for/while, try/catch, classes with methods — the treatment is briefer. The language’s standard library is unusually large; the Standard library and File I/O pages cover representative functions rather than aiming for completeness, and refer the reader to the official function index at mathworks.com/help/matlab for exhaustive coverage.