8.5. MATLAB Data Types#

Variables can store different types of data which are used to do different things.

Table 8.2 MATLAB Data Types#

Data type

Description

Integer

Integer values, e.g., -1, 0, 1, 99

Float

Floating point values (decimals), e.g., 1.5, 2.718, 3.1415927

Complex

Complex numbers, e.g., 1 + 2j (note the use of j for the imaginary number)

Boolean

Boolean values, e.g., true or false

String

Character string, e.g., "hello world"

MATLAB automatically use the appropriate data type for the value being assigned to a variable.

8.5.1. Casting data types#

We can change the data type for a variable using casting with the following functions

  • int32() - returns an 32-bit integer number from an input of a floating point number or string (similar functions are int8(), int16() and int64())

  • double() - returns a floating point number from an input of an integer or string

  • string() - returns a string from a range of data types.

Lets try casting between different data types. Enter the following commands into the console.

>> int32(1.23)

ans =

  int32

   1

>> double(123)

ans =

   123

>> string(123)

ans = 

    "123"

>> string(1 + 2j)

ans = 

    "1+2i"