MATLAB Variables and Data Types
Variables in MATLAB are created simply by assignment — there's no separate declaration step and no explicit type keyword, since MATLAB is dynamically typed and infers the type from the value on the right-hand side of '='; if you type an expression without assigning it to a name, MATLAB automatically stores the result in a special variable called ans.
Cricket analogy: Not needing to declare a variable's type before assigning it is like a scorer jotting down a batter's runs the moment the ball is hit, without first filling out a form declaring what kind of shot it will be — the type, four, six, or single, is inferred from what actually happens.
Numeric Types: double, single, and Integers
MATLAB's default numeric type is double (a 64-bit IEEE 754 floating-point number) — even typing x = 5 creates a double, not an integer — but you can explicitly create other numeric types like single, int8, uint8, int16, int32, and int64 by wrapping a value in the corresponding function, e.g. int32(5), which is important when working with image data (typically uint8, 0-255) or when you need to conserve memory on very large arrays.
Cricket analogy: Using double by default and only switching to int8 when needed is like a team defaulting to their standard Test-match XI but switching to a specialist night-watchman only when the situation specifically calls for it.
% Dynamic typing: type is inferred from the assigned value
x = 5; % double by default
class(x) % 'double'
% Explicit numeric types
px = uint8(200); % typical for image pixel data (0-255)
bigCount = int64(9000000000);
% Text types
name1 = 'Ada'; % char array
name2 = "Ada"; % string object
isequal(name1, name2) % true, values compare equal despite different types
% Logical type from comparison
isPassing = (75 >= 60) % logical: 1 (true)
% Struct: a single record with named fields
student.name = 'Priya';
student.grades = [88 92 79];
% Cell array: mixed types and sizes
mixedData = {'Priya', [88 92 79], true};Run whos in the Command Window (no arguments) to see every variable currently in your Workspace along with its size in bytes, dimensions, and class — this is the fastest way to spot an accidental type mismatch, like a double where you expected a logical.
Text, Logical, and Character Data
MATLAB has two text types: the older char array (single-quoted, e.g. 'hello', stored as a row vector of character codes) and the newer string type (double-quoted, e.g. "hello", introduced in R2016b, which supports array-of-strings semantics and functions like strsplit and contains more naturally); the logical type stores only true/false values and is what comparison operators like == and < return, and is also used for logical indexing.
Cricket analogy: The distinction between char and string types is like the difference between a scorecard written as loose individual chalk marks, a row of character codes, versus a proper digital scoring app that treats 'India 287/6' as one complete entry you can search and split.
Container Types: Cell Arrays and Structures
For data that isn't uniform in size or type, MATLAB provides the cell array (created with curly braces {}), which can hold different types and sizes in each element — a mix of strings, matrices, and numbers — and the struct, which groups related data under named fields accessed with dot notation (e.g. student.name, student.grades), making it the natural choice for representing a single record with several attributes.
Cricket analogy: A cell array is like a kit bag that can hold a bat, gloves, and a water bottle side by side even though they're completely different shapes and sizes, while a struct is like a player's official profile card with named fields, name, team, battingAverage, each holding a different kind of data.
Comparing text with == behaves differently for char and string: 'cat' == 'cat' returns a logical array (one comparison per character), while "cat" == "cat" returns a single true — mixing the two habits is a common source of subtle bugs, so prefer strcmp for char arrays and == only for string objects.
- MATLAB is dynamically typed -- no declaration keyword is needed, and type is inferred from the assigned value.
- double is the default numeric type; narrower types like int8/uint8 must be created explicitly.
- char arrays (single-quoted) and string objects (double-quoted) are distinct text types with different comparison behavior.
- logical is the type returned by comparison operators and used for logical indexing.
- Cell arrays ({}) hold mixed types and sizes; structs group named fields into one record via dot notation.
- whos lists every variable's size, dimensions, and class in the current Workspace.
- Use strcmp for char array comparisons; == works directly and safely only on string objects.
Practice what you learned
1. How does MATLAB determine a variable's type when you write x = 5;?
2. Which type would you typically use to store 8-bit image pixel data (0-255)?
3. What does 'cat' == 'cat' return in MATLAB, compared to "cat" == "cat"?
4. Which data structure lets you store a string, a numeric matrix, and a logical value together in one container, indexed positionally?
5. What command lists every variable currently in the Workspace along with size, dimensions, and class?
Was this page helpful?
You May Also Like
What Is MATLAB?
An introduction to MATLAB as a numerical computing environment and programming language, covering what it's used for and how it differs from general-purpose languages.
MATLAB Operators and Expressions
Arithmetic, relational, logical, and element-wise operators in MATLAB, and how operator precedence governs expression evaluation.
Your First MATLAB Script
How to create, save, and run a MATLAB script file, and the basic building blocks -- comments, output, and simple control flow -- used inside one.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics