Variables and Types in MATLAB
Learn about variables, data types, and type conversions in MATLAB with practical Docker-ready examples using GNU Octave
MATLAB’s type system reflects its origins as a matrix laboratory. Every value is fundamentally an array—a scalar is a 1x1 matrix, a string is a 1xN character array, and even a boolean is a 1x1 logical array. This array-first design means that operations you write for single values automatically extend to entire matrices without loops.
MATLAB is dynamically typed, so you never declare a variable’s type. You assign a value and MATLAB figures out the rest. It’s also weakly typed in the sense that numeric types mix freely—MATLAB will silently promote an integer to a double when arithmetic demands it. This makes exploratory computation fast but means you need to be aware of what types your variables actually hold.
In this tutorial, you’ll learn how MATLAB handles variables and assignments, explore the built-in numeric, string, and logical types, work with arrays and cell arrays, and see how type conversions work in practice.
Variable Basics and Numeric Types
MATLAB variables are created by simple assignment. There are no declaration keywords—just pick a name and assign a value. Variable names are case-sensitive, must start with a letter, and can contain letters, digits, and underscores.
Create a file named variables.m:
| |
Arrays, Strings, and Composite Types
In MATLAB, every variable is an array. Understanding how arrays, character arrays, cell arrays, and structs work is essential because these are the building blocks you’ll use for everything.
Create a file named variables_types.m:
| |
Running with Docker
| |
Expected Output
Output from variables.m:
x: 42 (double)
pi_approx: 3.14159 (double)
name: MATLAB (char)
is_ready: 1 (logical)
=== Numeric Types ===
a = 10 (double) - default is double
b = 10 (int32)
c = 255 (uint8)
d = 3.14 (single)
=== Special Values ===
Infinity: Inf
Negative infinity: -Inf
Not a number: NaN
Machine epsilon: 2.220446049250313e-16
Largest double: 1.797693e+308
Smallest positive double: 2.225074e-308
=== Complex Numbers ===
z1 = 3+4i
z2 = 1-2i
Real part of z1: 3.0
Imaginary part of z1: 4.0
Magnitude of z1: 5.0
=== Logical Values ===
true: 1 (logical)
false: 0 (logical)
logical(1): 1 (logical)
5 > 3 produces: 1 (logical)
Output from variables_types.m:
=== Character Arrays ===
char_arr: Hello (char, size: 1x5)
char_arr(1): H (ASCII: 72)
=== Matrices ===
Row vector: 1 2 3 4 5 (size: 1x5)
Column vector: 1 2 3 4 5 (size: 5x1)
Matrix (3x3):
1 2 3
4 5 6
7 8 9
Scalar 42 has size: 1x1
=== Cell Arrays ===
Cell array contents:
mixed{1}: 42 (double)
mixed{2}: hello (char)
mixed{3}: 3.14 (double)
mixed{4}: 1 (logical)
mixed{5}: 1 2 3 (double)
=== Structs ===
point.x = 3.0
point.y = 4.0
point.label = origin
Fields: x y label
=== Type Conversions ===
int32(3.7) = 4 (rounds to nearest)
double(int32(42)) = 42.0
str2double('123.45') = 123.45 (double)
num2str(42) = '42' (char)
logical(0) = 0, logical(1) = 1
double(true) = 1.0
=== Constants (by Convention) ===
MAX_ITERATIONS = 1000
GRAVITY = 9.81
Built-in pi = 3.1415926536
Built-in i (imaginary unit) = 0+1i
=== Empty and Special Values ===
Empty array size: 0x0
isempty([]): 1
isnan(NaN): 1
isinf(Inf): 1
isnumeric(42): 1
ischar('hello'): 1
islogical(true): 1
Key Concepts
- Everything is an array: A scalar is a 1x1 matrix, a string is a 1xN character array. This array-first design means operations naturally extend from scalars to matrices.
- Doubles by default: Every numeric literal is a
doubleunless you explicitly cast it withint32(),uint8(),single(), etc. - Dynamic and weakly typed: No type declarations needed. MATLAB freely converts between numeric types during arithmetic, promoting to
doublewhen needed. class()reveals the type: Useclass(x)to check any variable’s type, andwhosin the interactive workspace to see all variables with their sizes and types.- Cell arrays for mixed types: Regular arrays require all elements to be the same type. Cell arrays (
{}) can hold any mix of types—numbers, strings, other arrays, even other cell arrays. - Structs for named data: Use dot notation (
point.x = 3.0) to create structured data with named fields, similar to dictionaries in other languages. - No
constkeyword: MATLAB relies on naming conventions (UPPER_CASE) to signal constants. Built-in constants likepi,Inf,NaN, andepsare predefined. - Type-checking functions: MATLAB provides
isnumeric(),ischar(),islogical(),isempty(),isnan(), andisinf()to inspect values at runtime.
Running Today
All examples can be run using Docker:
docker pull gnuoctave/octave:9.4.0
Comments
Loading comments...
Leave a Comment