Skip to content

MATLAB - Data Types

MATLAB uses a dynamic type system, meaning you don’t need to declare a variable’s type before using it. MATLAB automatically allocates memory and determines the most appropriate data type. The fundamental data structure in MATLAB is the array, which can hold various types of data.

% MATLAB infers the type at assignment
myInteger = 42; % Inferred as 'double' by default
myText = "Hello World"; % Inferred as 'string'

Here are the most important data types for modern MATLAB programming:

CategoryData TypeDescription & Best Use Case
NumericdoubleDefault 64-bit double-precision floating-point. The standard choice for most numerical computations.
Numericsingle32-bit single-precision floating-point. Use when memory is limited and high precision is not required (e.g., large image data).
Numericint8, int16, int32, int64Signed integers of various sizes. Use for data that is naturally integer-valued (e.g., counters, indices) to save memory.
TextstringModern standard for text. An array of strings is flexible and supports a rich set of functions. Introduced in R2016b.
TextcharTraditional character array. A 'hello' is a 1x5 char array. Less flexible than string for text manipulation.
LogicallogicalRepresents true (1) or false (0). Used for indexing, masks, and control flow.
TabulartableContainer for mixed-type columnar data. Essential for data analysis. Variables can have different data types (e.g., numbers, text, categoricals).
TabulartimetableA specialized table that is indexed by time. Perfect for time-series data.
Containercell arrayAn array where each element (cell) can hold any other data type, including another array. Use when data is irregular and cannot fit in a table.
ContainerstructA structure with named fields, similar to objects. Good for grouping related data under a single variable name.
% 1. The 'string' type is preferred for text
names = ["Alice"; "Bob"; "Charlie"];
greetings = "Hello, " + names; % String arrays allow intuitive operations
% 2. The 'table' type for mixed data
lastName = ["Jones"; "Miller"; "Smith"];
age = [34; 45; 29];
isFullTime = [true; false; true];
employeeData = table(lastName, age, isFullTime, 'RowNames', names)
% Access data in a table easily
millersAge = employeeData.age("Bob")
% 3. Automatic Type Conversion
result = 5 + 3.14; % Integer (stored as double) + double -> double
% 4. Explicit Type Conversion
dataAsSingle = single(result);
textFromNumber = string(dataAsSingle);

You can convert between compatible data types and check a variable’s type programmatically.

  • Numeric to String: string(), num2str(), sprintf()
  • String to Numeric: str2double(), double()
  • Array to Table: array2table(), cell2table()
  • Table to Array: table2array(), table2cell()

Use class(variable) to get the type as a string, or is* functions for logical checks.

x_num = 10;
x_str = "10";
x_log = true;
% Get the class name
disp(['The class of x_num is: ', class(x_num)]); % Output: double
% Check the type with is* functions
if isnumeric(x_num)
disp('x_num is numeric.');
end
if isstring(x_str)
disp('x_str is a string.');
end
% Programmatic handling of types
switch class(x_log)
case 'double'
disp('Variable is a double.');
case 'logical'
disp('Variable is a logical.');
otherwise
disp('Variable is of another type.');
end
  • Use double by default for all floating-point math unless you have specific memory or precision requirements.
  • Strongly prefer string arrays over character arrays for any text manipulation task. They are more powerful and intuitive.
  • Use table and timetable for any tabular or experimental data. They make your code cleaner, more readable, and less error-prone than using numeric matrices or cell arrays.
  • Use integers (int32, etc.) only when you are certain the data fits and memory savings are significant. MATLAB’s core math functions are optimized for double.