Skip to main content
Logo image

Subsection Naming Variables

Valid Names.

MATLAB imposes specific rules on variable names:
  1. The name must start with a letter.
  2. It may contain letters, digits, and underscores.
  3. It cannot be a MATLAB keyword.
  4. It cannot exceed 2048 characters.
Things to note about MATLAB variables:
  • Variable names are case sensitive, so A and a are different variables.
  • There are 20 keywords that can be listed with the iskeyword command.
Table 11. MATLAB keywords listed from iskeyword
function if else elseif end
try switch case otherwise catch
for while break continue return
classdef parfor persistent spmd global

Checkpoint 12.

Choosing Good Names.

In practice, variable names should clearly describe the data they store. Descriptive names improve readability and reduce errors.
Once you begin writing scripts, good names make your code easier to read later and share with others.
price = 3.11;
tankCapacity = 15;
fullTankCost = price * tankCapacity;

Checkpoint 13.

What is the primary advantage of using descriptive variable names in MATLAB code?
  • They make code easier to read, understand, and debug.
  • They make MATLAB run faster.
  • They prevent variables from being overwritten.
  • They allow MATLAB to automatically document the code.