MATLAB - Data Types

Advertisements


MATLAB does not require any type declaration or dimension statements. Whenever MATLAB encounters a new variable name, it creates the variable and allocates appropriate memory space.

If the variable already exists, then MATLAB replaces the original content with new content and allocates new storage space, where necessary.

For example,

Total = 42

The above statement creates a 1-by-1 matrix named 'Total' and stores the value 42 in it.

Data Types Available in MATLAB

MATLAB provides 15 fundamental data types. Every data type stores data that is in the form of a matrix or array. The size of this matrix or array is a minimum of 0-by-0 and this can grow up to a matrix or array of any size.

The following table shows the most commonly used data types in MATLAB:

Data Type Description
int88-bit signed integer
uint88-bit unsigned integer
int1616-bit signed integer
uint1616-bit unsigned integer
int3232-bit signed integer
uint3232-bit unsigned integer
int6464-bit signed integer
uint6464-bit unsigned integer
singlesingle precision numerical data
doubledouble precision numerical data
logicallogical values of 1 or 0, represent true and false respectively
charcharacter data (strings are stored as vector of characters)
cell arrayarray of indexed cells, each capable of storing an array of a different dimension and data type
structureC-like structures, each structure having named fields capable of storing an array of a different dimension and data type
function handlepointer to a function
user classesobjects constructed from a user-defined class
java classesobjects constructed from a Java class

Example

Create a script file with the following code:

str = 'Hello World!'
n = 2345
d = double(n)
un = uint32(789.50)
rn = 5678.92347
c = int32(rn)

When the above code is compiled and executed, it produces the following result:

str =
Hello World!
n =
   2345
d =
   2345
un =
   790
rn =
   5.6789e+03
c =
   5679

Data Type Conversion

MATLAB provides various functions for converting from one data type to another. The following table shows the data type conversion functions:

FunctionPurpose
charConvert to character array (string)
int2str Convert integer data to string
mat2strConvert matrix to string
num2strConvert number to string
str2doubleConvert string to double-precision value
str2numConvert string to number
native2unicodeConvert numeric bytes to Unicode characters
unicode2nativeConvert Unicode characters to numeric bytes
base2decConvert base N number string to decimal number
bin2decConvert binary number string to decimal number
dec2baseConvert decimal to base N number in string
dec2binConvert decimal to binary number in string
dec2hex Convert decimal to hexadecimal number in string
hex2dec Convert hexadecimal number string to decimal number
hex2num Convert hexadecimal number string to double-precision number
num2hex Convert singles and doubles to IEEE hexadecimal strings
cell2matConvert cell array to numeric array
cell2structConvert cell array to structure array
cellstr Create cell array of strings from character array
mat2cell Convert array to cell array with potentially different sized cells
num2cell Convert array to cell array with consistently sized cells
struct2cellConvert structure to cell array

Determination of Data Types

MATLAB provides various functions for identifying data type of a variable.

Following table provides the functions for determining the data type of a variable:

Function Purpose
isDetect state
isaDetermine if input is object of specified class
iscellDetermine whether input is cell array
iscellstrDetermine whether input is cell array of strings
ischarDetermine whether item is character array
isfieldDetermine whether input is structure array field
isfloatDetermine if input is floating-point array
ishghandleTrue for Handle Graphics object handles
isintegerDetermine if input is integer array
isjavaDetermine if input is Java object
islogicalDetermine if input is logical array
isnumericDetermine if input is numeric array
isobjectDetermine if input is MATLAB object
isrealCheck if input is real array
isscalarDetermine whether input is scalar
isstrDetermine whether input is character array
isstructDetermine whether input is structure array
isvectorDetermine whether input is vector
classDetermine class of object
validateattributesCheck validity of array
whosList variables in workspace, with sizes and types

Example

Create a script file with the following code:

x = 3
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
 
x = 23.54
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
 
x = [1 2 3]
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
 
x = 'Hello'
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)

When you run the file, it produces the following result:

x =
     3
ans =
     0
ans =
     1
ans =
     1
ans =
     1
ans =
     1
x =
   23.5400
ans =
     0
ans =
     1
ans =
     1
ans =
     1
ans =
     1
x =
     1     2     3
ans =
     0
ans =
     1
ans =
     1
ans =
     0
x =
Hello
ans =
     0
ans =
     0
ans =
     1
ans =
     0
ans =
     0


Advertisements
Advertisements