Difficulty: Beginner | Prerequisites: None, though basic familiarity with any programming language or a scientific calculator is helpful.
MATLAB (short for matrix laboratory) is a numerical computing environment widely used in engineering, science, and applied mathematics courses. This lecture introduces the basics: what MATLAB is, how to navigate its interface, how to perform arithmetic, and how to work with variables and built-in functions. If you have used a graphing calculator or written simple code in any language, the concepts here will feel familiar. If not, this is a clean starting point with no prior programming assumed.
MATLAB is an interpreted, matrix-optimised language you can use as a powerful calculator from the Command Window. Variables are created by assignment (no declaration needed), names are case-sensitive, and a semicolon at the end of a line suppresses output. The environment includes a Current Folder pane, a Command Window for interactive use, and a Workspace that tracks your variables between commands.
MATLAB (matrix laboratory)
A numerical computing environment developed by Cleve Moler in the 1970s, optimised for matrix operations. Originally derived from FORTRAN, later rewritten in C.
In simple terms, think of it as a very powerful scientific calculator that can also write and run programs.
Interpreted language
A language whose commands are executed line by line at run time, rather than being compiled into machine code first.
In simple terms, you type a command and MATLAB runs it immediately, no separate compile step needed.
Command Window
The main interactive pane in MATLAB where you type expressions after the prompt (>>) and see results immediately.
Think of it as the calculator screen where you do your work.
Workspace
The area of memory where MATLAB stores all current variables and their values. Contents persist between commands within a session.
In simple terms, it is a running record of every variable you have created so far.
Current Folder
The file-browser pane showing the directory MATLAB is currently working in. Scripts and data files need to be in (or on the path of) this folder to run.
M-file (script file)
A plain-text file with a .m extension containing a sequence of MATLAB statements. Created in the MATLAB editor or from the command line with edit filename.m.
Think of it as a saved list of calculator keystrokes you can replay.
64-bit double
The default numeric data type in MATLAB. Every number you type is stored as a double-precision floating-point value (about 15 significant digits).
ans
A built-in variable that automatically stores the result of the most recent unassigned expression, similar to the "Ans" key on a calculator.
NaN (Not a Number)
A special value representing an undefined or unrepresentable numerical result, such as 0/0.
eps
The smallest difference between two distinguishable double-precision numbers. A measure of floating-point relative accuracy.
mod(a, m)
The modulo function, returning the remainder after dividing a by m. Internally computed as a - m.*floor(a./m).
Developed by Cleve Moler in the 1970s, originally derived from FORTRAN, later rewritten in C with graphic-plotting capabilities added.
Optimised for matrix and vector operations. It is not a general-purpose programming language in the way that Python or C++ are.
An interpreted language: commands execute line by line. No compilation step.
All numbers are 64-bit doubles by default.
Current Folder pane: shows files in the working directory. Right-click to create new folders, or type mkdir foldername in the Command Window.
Command Window: the main workspace. Type expressions after the >> prompt and results appear immediately.
Workspace pane: displays all variables currently in memory with their names and values. Contents persist between commands, which can cause unintended side effects across problems.
Use clear all at the start of each new calculation to wipe the workspace clean.
+ Addition
- Subtraction
* Multiplication
/ Division (right division, e.g. 2/5 gives 0.4)
^ Power / exponentiation (e.g. 2^3 gives 8)
mod(a, m) Modulo (remainder after division)
Parentheses are evaluated first, innermost outward.
Exponents are evaluated next, left to right.
Multiplication and division, left to right.
Addition and subtraction, left to right.
Worked example: the expression 1/(2+3^2)+4/5*6/7 evaluates to 0.7766. Trace it step by step: 3^2 = 9, then 2+9 = 11, then 1/11, then 4/5 = 0.8, then 0.8*6 = 4.8, then 4.8/7, and finally the sum.
MATLAB computes in double precision (about 15 significant digits) but displays only 4 decimal places by default.
format long shows the full 15-digit result.
format short returns to the 4-decimal default.
clc clears the Command Window (does not delete variables).
Variables persist in the Workspace between commands. One problem's leftover variables can silently affect the next.
Best practice: start every new calculation with clear all.
Use the Workspace pane or whos to inspect what is currently stored.
Operator precedence (highest to lowest):
Parentheses → Exponents → Multiplication / Division → Addition / SubtractionModulo function:
b = mod(a, m)
b = a - m .* floor(a ./ m)Display format commands:
format long % 15-digit display
format short % 4-decimal display (default)
clc % clear Command Window
clear all % clear all Workspace variablesMATLAB is the standard tool in many engineering and science departments for data analysis, simulation, and prototyping. If you go on to work with signal processing, control systems, image analysis, or numerical simulation, you will almost certainly encounter MATLAB or its open-source counterpart GNU Octave. The arithmetic and variable concepts here are the foundation for every script and function you will write later.
Students often think MATLAB is a general-purpose programming language like Python. It is not. It is purpose-built for numerical and matrix computation, and while you can do many things in it, it is not the right tool for web development, system programming, or general software engineering.
Students assume that i and j are safe variable names for loop counters. They are not, because MATLAB reserves i and j for the imaginary unit. Use ii and jj instead to avoid silent bugs in calculations involving complex numbers.
Students forget that the Workspace persists between commands. A variable left over from a previous problem can produce wrong results in the next one without any error message. Always clear all at the start.
Students confuse clc (clears the Command Window display) with clear all (deletes all variables from the Workspace). The first is cosmetic, the second is functional.
Operator precedence is a classic exam question. Be able to evaluate expressions like 1/(2+3^2)+4/5*6/7 by hand, step by step.
Know the difference between format long and format short. This controls display only, not the underlying precision of the calculation.
Know the built-in variables (ans, eps, pi, Inf, NaN, i, j) and why you should avoid overwriting them.
Understand that the semicolon (;) suppresses output but does not change the computation. Exams may ask what a block of code displays, and the answer depends on which lines have semicolons.
Be comfortable with the mod function and how it relates to floor.
True or false: MATLAB is a compiled language. (False, it is interpreted.)
What is the default numeric data type in MATLAB? (64-bit double.)
Fill in the blank: To clear all variables from the Workspace, type ________. (clear all)
True or false: clc deletes your variables. (False, it only clears the Command Window display.)
What value does 2^3 + 4/2 return? (10.)
Q: Evaluate the expression 3 + 4 * 2 / (1 - 5)^2 in MATLAB. What is the result?
A: 3.5. Parentheses first: 1 - 5 = -4. Exponent: (-4)^2 = 16. Multiplication: 4 * 2 = 8. Division: 8 / 16 = 0.5. Addition: 3 + 0.5 = 3.5.
Q: You type x = 10; in the Command Window. What appears on screen?
A: Nothing. The semicolon suppresses the display of the result. The variable x is still created and stored in the Workspace.
Q: What is the difference between format long and format short?
A: format long displays results to about 15 significant digits. format short (the default) displays 4 decimal places. Neither changes the internal precision of the computation, which is always double precision.
Q: Why should you avoid using i as a loop variable in MATLAB?
A: Because i (and j) are reserved as the imaginary unit for complex numbers. Overwriting them can silently break calculations that involve complex arithmetic. Use ii or jj instead.
Q: What does mod(17, 5) return, and why?
A: 2. The modulo function returns the remainder after dividing 17 by 5. Since 17 = 3 * 5 + 2, the remainder is 2.
This material connects directly to later lectures on vectors, matrices, and array operations, where the arithmetic operators introduced here get element-wise counterparts (.*, ./, .^). The variable and workspace concepts carry through to every MATLAB script and function you will write in the course. Understanding operator precedence is also foundational for writing correct conditional expressions in if statements and loops.
MATLAB introduction, matrix laboratory, COE 301, MATLAB arithmetic operators, MATLAB operator precedence, MATLAB Command Window, MATLAB Workspace, MATLAB Current Folder, format long, format short, clc, clear all, mod function, modulo MATLAB, 64-bit double, MATLAB built-in variables, ans, eps, pi, Inf, NaN, M-file, MATLAB script, MATLAB calculator, Cleve Moler, MATLAB environment overview