Variables and Data Types

A variable is created by assignment: speed = 120.

Concept

A variable is created by assignment: speed = 120. Names start with a letter, contain letters, digits and underscores, are case-sensitive, and can be up to namelengthmax (63) characters. Avoid names of built-in functions (i, j, sum, max, length) — they shadow the functions.

Everything Is an Array A scalar is a 1×1 array, a row vector 1×n, a matrix m×n. size, numel, class and whos describe any variable.

Default for all numbers; about 15–16 significant digits About 7 digits; saves memory (GPU, large arrays) int8/16/32/64, uint8/16/32/64

Integers. Arithmetic saturates at the limits (no wrap-around) and rounds results true/false (1/0) Characters (Module 8)

Any numeric class can be complex: 3 + 4i Special values: Inf, -Inf, NaN (not a number), eps (spacing of doubles near 1), realmax, realmin, intmax, intmin, pi, i/j (imaginary unit).

Conversion: double(x), int16(x), logical(x), num2str, str2double (Module 8). Mixed arithmetic between an integer class and a double gives the integer class.

Why It Matters

Integer saturation, floating-point rounding and unexpected classes cause silent errors in data from sensors and ECUs (which often arrive as uint8/uint16). Knowing the class of your data is the first debugging step.

Syntax

x = 3.14;               % double
n = int16(-300);        % 16-bit integer
b = true;               % logical
z = 2 - 1i;             % complex
class(x), size(x), whos

Example

2.1 Numeric classes, saturation, special values

% Every value is an array; the default numeric type is double
x = 42;
fprintf('x = %g, class %s, size %dx%d\n', x, class(x), size(x, 1), size(x, 2))

y = int8(100);                     % integer types saturate instead of wrapping
z = y + 100;
fprintf('int8(100) + 100 = %d (saturates at intmax(''int8'') = %d)\n', z, intmax('int8'))

u = uint8(5) - 10;
fprintf('uint8(5) - 10 = %d (saturates at 0)\n', u)

r = int32(7) / int32(2);           % integer division rounds to nearest
fprintf('int32(7)/int32(2) = %d (rounded), idivide gives %d\n', r, idivide(int32(7), int32(2)))

f = single(1) / 3;
fprintf('single 1/3 = %.10f, double 1/3 = %.10f\n', f, 1/3)

b = true;
c = 'A';
fprintf('class(true) = %s, class(''A'') = %s, double(''A'') = %d\n', class(b), class(c), double(c))

zc = 3 + 4i;
fprintf('complex %s: abs = %g, angle = %.4f rad, real = %g, imag = %g\n', num2str(zc), abs(zc), angle(zc), real(zc), imag(zc))

fprintf('eps = %g, realmax = %g, intmax(''int32'') = %d\n', eps, realmax, intmax('int32'))
fprintf('1/0 = %g, -1/0 = %g, 0/0 = %g\n', 1/0, -1/0, 0/0)
fprintf('0.1 + 0.2 == 0.3 is %d, abs difference %g\n', 0.1 + 0.2 == 0.3, abs(0.1 + 0.2 - 0.3))

a = 5; A = 6;                      % n
…

Expected Result

Integer arithmetic saturated (int8 stops at 127, uint8 at 0) and int32(7)/int32(2) rounded to 4, while idivide truncates toward zero by default. 0.1 + 0.2 == 0.3 is false because of binary rounding — compare floating-point values with a tolerance. The whos table (Octave's layout; MATLAB's columns are similar) shows each variable's size, bytes and class.

x = 42, class double, size 1x1
int8(100) + 100 = 127 (saturates at intmax('int8') = 127)
uint8(5) - 10 = 0 (saturates at 0)
int32(7)/int32(2) = 4 (rounded), idivide gives 3
single 1/3 = 0.3333333433, double 1/3 = 0.3333333333
class(true) = logical, class('A') = char, double('A') = 65
complex 3+4i: abs = 5, angle = 0.9273 rad, real = 3, imag = 4
eps = 2.22045e-16, realmax = 1.79769e+308, intmax('int32') = 2147483647
1/0 = Inf, -1/0 = -Inf, 0/0 = NaN
0.1 + 0.2 == 0.3 is 0, abs difference 5.55112e-17
a = 5, A = 6
Variables visible from the current scope:

variables in scope: top scope

  Attr   Name        Size                     Bytes  Class
  ====   ====        ====                     =====  ===== 
         a           1x1                          8  double
         A           1x1                          8  double
         x           1x1                          8  double
         y           1x1                          1  int8
  c      zc          1x1                         16  double

Total is 5 elements using 41 bytes

Common Mistakes

Using i or j as loop variables and later expecting them to be the imaginary unit.

Doing arithmetic on uint8 sensor data and getting saturated or rounded results; convert with double() first.

Comparing floating-point numbers with ==.

Assuming int32(7)/2 truncates like C.

Best Practices

Keep computations in double; convert integer data at the boundary.

Use abs(a - b) < tol for floating-point comparisons.

Use meaningful names with units (speed_kmh, temp_degC).

Interview Relevance

Medium: "default data type?", "what happens on integer overflow in MATLAB?", "why is 0.1 + 0.2 ~= 0.3?", "what is eps?".

Practice Questions

What are intmax('uint16') and intmin('int16')?

Compute uint8(200) + uint8(100) and uint8(200) - uint8(250).

Show the difference between single(pi) and pi with fprintf('%.10f').

Create a complex number and compute its magnitude and phase in degrees.

Quick Quiz

Q1. (Multiple choice) The default numeric class in MATLAB is: A) int32 B) single C) double D) float

Answer: C (verified: class(x) is double).

Q2. (Predict the output) int8(100) + 100 Answer: 127 — saturation (verified).

Q3. (True/False) int32(7) / int32(2) returns 3.

Answer: False. It rounds to 4 (verified); use idivide to truncate.

Q4. (Find the bug) for i = 1:3, ... end; z = 2 + 3*i; gives z = 11 instead of a complex number.

Answer: The loop overwrote i with 3. Use 1i for the imaginary unit and other loop variable names.

Coding Challenge

Given raw 8-bit sensor readings uint8([200 220 250]), show that adding two of them in uint8 saturates, and compute their average correctly as a double with two decimals.

raw = uint8([200 220 250]);
naive = mean(raw);                       % mean converts to double internally
fprintf('mean(raw) = %.2f, class %s\n', naive, class(naive))
fprintf('raw(1) + raw(2) in uint8 = %d (saturated)\n', raw(1) + raw(2))
correct = sum(double(raw)) / numel(raw);
fprintf('average via double = %.2f\n', correct)