TExcelTAD
Home › MATLAB / Octave › Data Types & Classes

Data Types & Classes

Every value in MATLAB belongs to a class (its data type). You rarely have to think about it, because double is the default for numbers, but knowing the other classes helps when memory, exact integers or text matter.

Class Holds Example
double 64-bit floating point (default) 3.75
single 32-bit floating point single(3.75)
int8 … int64, uint8 … uint64 Signed/unsigned integers uint8(200)
logical true / false 5 > 3
char Characters (text) 'hello'
cell Containers for mixed data {1, 'a'}
struct Named fields s.name = 'Ann'
function_handle A reference to a function @sin

Asking for the class

class(x) returns the class name as text, and isa(x, name) checks it. There are also quick tests like isnumeric, ischar, islogical, iscell and isstruct:

matlab · try it yourself
items = {42, 'text', true, int16(7), {1}, @sqrt, single(2.5)};
for k = 1:numel(items)
    fprintf('%-16s numeric=%d\n', class(items{k}), isnumeric(items{k}));
end
disp(isa(int16(7), 'integer'))
disp(isa(single(2.5), 'float'))
double           numeric=1
char             numeric=0
logical          numeric=0
int16            numeric=1
cell             numeric=0
function_handle  numeric=0
single           numeric=1
1
1

isa also understands the category names 'numeric', 'float' and 'integer'.

Integer types

Integer classes store whole numbers in a fixed number of bits. They are used for images (uint8 pixels from 0 to 255), hardware data and when you need exact counting. Two behaviours surprise newcomers:

  1. Conversion rounds to the nearest integer (halves away from zero).
  2. Arithmetic saturates: results outside the range are clamped to the minimum or maximum instead of wrapping around.
matlab · try it yourself
disp(int8(3.5))
disp(int8(-3.5))
disp(uint8(250) + 10)
disp(uint8(5) - 10)
disp(int8(-100) * 2)
fprintf('int8 range: %d to %d\n', intmin('int8'), intmax('int8'));
fprintf('uint16 max: %d\n', intmax('uint16'));
4
-4
255
0
-128
int8 range: -128 to 127
uint16 max: 65535

Integer division also rounds instead of truncating: int32(7) / int32(2) is 4. Use idivide when you want other rounding:

matlab · try it yourself
disp(int32(7) / int32(2))
disp(idivide(int32(7), int32(2)))
disp(class(int32(7) * 2.6))
disp(int32(7) * 2.6)
4
3
int32
18

Mixing an integer with a double gives an integer result (the calculation happens in double precision and is then rounded back). Mixing two different integer classes, such as int8 and int16, is an error.

Converting between classes

Call the class name as a function to convert: double(x), int16(x), char(x), logical(x). Text and numbers convert through character codes:

matlab · try it yourself
codes = double('ABC')
letters = char([72 105 33])
flags = logical([2 0 -1])
asText = num2str(3.75)
back = str2double('3.75') * 2
codes =

   65   66   67

letters = Hi!
flags =

  1  0  1

asText = 3.75
back = 7.5000

Note: double('3.75') gives the character codes [51 46 55 53], not the number 3.75. To parse text as a number use str2double.

single vs double

single uses half the memory but keeps only about 7 significant digits. Mixing a single with a double gives a single:

matlab · try it yourself
x = single(1) / 3;
y = 1 / 3;
fprintf('single: %.12f\n', x);
fprintf('double: %.12f\n', y);
disp(class(x + y))
single: 0.333333343267
double: 0.333333333333
single

Stick with double unless you have a good reason; most functions expect it.

Test yourself

What is uint8(250) + 10?

  1. 260
  2. 4
  3. 255
  4. An overflow error

What does class(5 > 3) return?

  1. 'double'
  2. 'logical'
  3. 'int8'
  4. 'char'

Which call turns the text '12.5' into the number 12.5?

  1. double('12.5')
  2. str2double('12.5')
  3. char(12.5)
  4. int8('12.5')