MATLAB Data Types

MATLAB exposes numeric, character array, and string data types.


Numeric

All numeric variables are arrays. Colloquially, an array with a single element is called a scalar.

By default, all numeric values are stored as double-precision floating point. Other storage sizes are available however.

x = 3;
y = [3 5];

A shorthand for an array of sequential numbers (with an optional step parameter) is:

a = 5:8;
b = 20:2:24;


Storage Size

To declare a numeric array with a specific storage size, try:

y = uint64(10);

These declaration functions include:


Type Casting

The cast function is used to change the storage size of a numeric array. Try:

b = cast(a,"uint8");

The second argument must be one of:


Character Array

Character arrays store a sequence of characters. They are declared like:

c = 'Hello World';


String

All string variables are arrays. Colloquially, a string array with a single element is called a string scalar.

str = "Greetings friend"
str = ["Greetings friend" "Hello, world"]

To convert a numeric array into a string array, try:

>> str = string([1 24 356]);

str = 1x3 string

   "1"          "24"          "356"


CategoryRicottone