Skip to content

MATLAB - Predefined Variables

MATLAB - Built-in Constants and Special Values

Section titled “MATLAB - Built-in Constants and Special Values”

MATLAB provides a set of built-in names that represent common mathematical constants and special values. While you can overwrite these with your own variable assignments, it is strongly discouraged as a best practice. Doing so can lead to confusing and hard-to-debug errors, especially when using functions that rely on these standard values.

These are the most common built-in values you will encounter.

NameDescription and Modern Best Practice
piRepresents the mathematical constant π (≈ 3.14159). It is a double-precision floating-point number.
i, jRepresent the imaginary unit, where i² = -1. Best Practice: Use 1i or 1j (e.g., z = 3 + 4*1i) to create complex numbers. This prevents issues if you accidentally use i or j as a loop counter.
infRepresents infinity. It typically results from operations like 1/0 or log(0).
NaNStands for ‘Not a Number’. It results from mathematically undefined operations, such as 0/0 or inf - inf.
epsThe ‘floating-point relative accuracy’. It is the distance from 1.0 to the next largest representable double-precision number. Useful for numerical stability checks. You can also use eps(x) to find the gap at a value x.
% Calculate the area of a circle with radius 5
radius = 5;
area = pi * radius^2;
% Create a complex number using the recommended syntax
complex_num = -2 + 1i * 5;
% Demonstrate how NaN and Inf can arise
val1 = 0/0; % Results in NaN
val2 = 1/0; % Results in Inf
disp(['Area: ', num2str(area)])
disp(['Complex Number: ', num2str(complex_num)])
disp(['0/0 is: ', num2str(val1)])
disp(['1/0 is: ', num2str(val2)])

Expected Output:

Area: 78.5398
Complex Number: -2+5i
0/0 is: NaN
1/0 is: Inf
NameDescription
ansA temporary variable that holds the result of the most recent command that was not assigned to a variable. It’s useful for quick calculations in the Command Window but should not be relied upon in scripts.
% In the command window...
5 * 10
ans =
50
ans / 2
ans =
25

The original tutorial listed some commands and keywords as variables. It’s important to distinguish them correctly.

Command/KeywordDescription
whosA command that lists all variables in the current workspace, along with their size, memory usage, and data type.
globalA keyword used to declare a variable that can be accessed across different functions and the base workspace. Use with caution as it can make code harder to debug.
% Create some variables of different types
A = rand(3, 3); % A 3x3 double matrix
S = "Hello, World!"; % A string scalar
C = {1, 'two', [3,3]}; % A cell array
% Display the workspace contents
whos

Expected Output (will vary slightly):

Name Size Bytes Class Attributes
A 3x3 72 double
C 1x3 266 cell
S 1x1 174 string

Deprecation Notice: The legacy functions date and clock are not recommended for new code. They are less flexible and can be ambiguous.

The modern and recommended way to handle date and time information is with the datetime object.

% Get the current date and time
now_dt = datetime('now')
% Get just today's date (time is midnight)
today_dt = datetime('today')
% You can perform arithmetic and formatting easily
yesterday_dt = today_dt - days(1);
disp(['Yesterday was: ', datestr(yesterday_dt)])

Expected Output (will reflect the current date):

now_dt =
datetime
15-Oct-2024 10:30:00
today_dt =
datetime
15-Oct-2024
Yesterday was: 14-Oct-2024