02 · Variables & Basic Data Types¶
Assignment¶
MATLAB assignment uses =, and variable names are created the moment you
first assign to them — no declaration keyword needed:
Variable names must start with a letter, can contain letters, digits, and
underscores, and are case-sensitive — Age and age are two different
variables. By convention MATLAB code favors camelCase or lower_snake
names; reserved words like if, for, and end cannot be used as variable
names.
Numeric types¶
By default, every number in MATLAB is a double (64-bit floating point) —
unlike C or Java, you don't choose int vs float unless you specifically
need to:
Both 5 and 5.5 are the same underlying type. Integer types exist
(int8, int16, int32, int64, and their uint* unsigned counterparts)
but must be created explicitly, and are mainly used when interfacing with
hardware, embedded targets, or memory-constrained data:
>> z = int32(5);
>> class(z)
ans =
'int32'
>> z + 2.9 % integer types round arithmetic results, they don't truncate
ans =
int32
8
int32(5) + 2.9 rounds to 8 (5 + 2.9 = 7.9, rounded to the nearest
integer), not truncated to 7 — a subtlety worth remembering if you ever
mix integer and double arithmetic.
char vs string — MATLAB has two text types¶
This trips up almost everyone coming from another language. MATLAB has
both a legacy char array type (single quotes) and a newer string
type (double quotes, added in R2016b) that behave differently:
>> a = 'hello'; % char array
>> class(a)
ans =
'char'
>> b = "hello"; % string
>> class(b)
ans =
'string'
A char array is really a row vector of character codes — indexing it
gives you individual characters, and concatenating two char values with
different treatment than strings requires strcat or square brackets:
A string behaves more like text in other modern languages — you can
concatenate with +, and a string array can hold multiple independent
text elements, each of arbitrary length, like a cell array of strings but
with cleaner syntax:
>> "hello" + " " + "world"
ans =
"hello world"
>> names = ["Alice", "Bob", "Carol"];
>> names(2)
ans =
"Bob"
Which one should you use?
New code should generally prefer string (double quotes) — it has
cleaner semantics and better error messages. But a huge amount of
existing MATLAB code, and many built-in functions (like fieldnames,
error identifiers, and older file I/O functions), still expect or
return char. Expect to see both, and know that string(x) and
char(x) convert between them.
Logical (boolean) type¶
A logical displays as 1/0, not true/false, but class() confirms
it's a distinct type from double — this matters because logical arrays
are used directly for indexing (covered in Module 03).
Checking and converting types¶
>> x = 5;
>> isa(x, 'double')
ans =
logical
1
>> isnumeric(x)
ans =
logical
1
>> ischar('hello')
ans =
logical
1
>> isstring("hello")
ans =
logical
1
Conversion functions follow a consistent type(value) naming pattern:
>> num2str(42) % number -> char, e.g. for building messages
ans =
'42'
>> str2double('3.14') % text -> double
ans =
3.1400
>> str2num('42') % text -> double, but evaluates as MATLAB code (avoid on untrusted input)
ans =
42
str2double is generally safer than str2num for parsing plain numeric
text, since str2num runs its input through MATLAB's interpreter — fine for
'42', but a risk if the text ever comes from an untrusted source.
The whos command — inspecting what's in memory¶
>> x = 5; name = "Alice"; flag = true;
>> whos
Name Size Bytes Class Attributes
flag 1x1 1 logical
name 1x1 142 string
x 1x1 8 double
whos is the fastest way to answer "wait, what type is this variable
again, and how big is it" without printing the whole value.
Type cheat sheet¶
| Type | Created with | class() result |
|---|---|---|
| Double (default numeric) | 5, 5.5 |
double |
| Integer (explicit) | int32(5), uint8(5) |
int32, uint8, etc. |
| Char array | 'hello' |
char |
| String | "hello" |
string |
| Logical | true, 5 > 3 |
logical |
How It Actually Works¶
Every variable you create lives in a workspace — a hash table mapping
names to array headers — and assignment never mutates a value in place;
MATLAB uses copy-on-write. When you write b = a, MATLAB does not
duplicate a's underlying data buffer; both a and b point at the same
memory with a reference count of 2. Only when one of them is modified
(a(1) = 99) does MATLAB allocate a fresh buffer for the one being
changed, decrement the shared buffer's refcount, and copy the data over.
This is why passing large arrays into functions is cheap in MATLAB even
though the language has value semantics — the copy is deferred until it's
actually needed (lazy/copy-on-write copying), not performed eagerly at the
assignment or function-call boundary.
The double you get by default is IEEE 754 binary64: 1 sign bit, 11
exponent bits, 52 mantissa bits, giving roughly 15-17 significant decimal
digits and a machine epsilon (eps in MATLAB) of about 2.22e-16. The
int32(5) + 2.9 = 8 rounding behavior isn't a quirk — MATLAB's integer
classes implement saturating, round-to-nearest arithmetic by
specification: any arithmetic result on a fixed-width integer type is
computed at higher precision internally, then rounded to the nearest
representable integer (ties away from zero) and clamped to the type's
range rather than wrapping around on overflow, unlike C's integer types.
char and string differ at the memory layout level too: a char array
is literally a vector of UTF-16 code units (each char is a 2-byte
integer under the hood, accessible via double('A') giving 65), while
string is a reference-counted object array where each element can hold
independently-sized UTF-16 text — closer to a cell of strings with
optimized storage than to a primitive array.
Note: reasoned from MATLAB's documented type system and IEEE 754 semantics; not run in MATLAB itself.
🔀 See this in another language¶
Exercise¶
Create a variable temperature holding 98.6 and confirm its class is
double. Create a char variable city = 'Boston' and a string variable
country = "USA"; concatenate them into one message reading "Boston, USA"
using square-bracket char concatenation for one version and + string
concatenation for another. Finally, use str2double to convert the text
'451' into a number and add 10 to confirm it behaves as a true numeric
value (not text).