English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
MATLAB does not require any type declaration or dimension statement. Whenever MATLAB encounters a new variable name, it creates a variable and allocates appropriate memory space.
If a variable already exists, MATLAB will replace the original content with new content and allocate new storage space if necessary.
For example,
Total = 42
The following statement creates a named “Total”1than1matrix, and assign values42stored in it.
MATLAB provides15types of basic data types. Each data type stores data in matrix or array form. The size of this matrix or array can be as small as 0×0, and can grow to any size of matrix or array.
The following table shows the most commonly used data types in MATLAB-
Serial number | Data type and description |
---|---|
1 | int8 8signed integer |
2 | uint8 8unsigned integer |
3 | int16 16signed integer |
4 | uint16 16unsigned integer |
5 | int32 32signed integer |
6 | uint32 32unsigned integer |
7 | int64 64signed integer |
8 | uint64 64unsigned integer |
9 | single Single precision numerical data |
10 | double Double precision numerical data |
11 | logical Logical value1or 0, respectively representing true and false |
12 | char Character data (strings stored as character vectors) |
13 | cell array Index cell array, each cell can store arrays of different dimensions and data types |
14 | structure Similar to C structures, each structure has named fields that can store arrays of different dimensions and data types |
15 | function handle Pointer to a function |
16 | user classes Objects constructed from user-defined classes |
17 | java classes Objects constructed from Java classes |
Create a script file using the following code-
str = 'Hello World!' n = 2345 d = double(n) un = uint32(789.50) rn = 5678.92347 c = int32(rn)
After compiling and executing the above code, the following results will be produced-
str = Hello World! n = 2345 d = 2345 un = 790 rn = 5678.9 c = 5679
MATLAB provides various functions to convert values from one data type to another. The following table shows the data type conversion functions-
Function | Function |
---|---|
char | Convert to character array (string) |
int2str | Convert integer data to string |
mat2str | Convert matrix to string |
num2str | Convert number to string |
str2double | Convert string to double precision value |
str2num | Convert string to number |
native2unicode | Convert numeric byte to Unicode character |
unicode2native | Convert Unicode character to numeric byte |
base2dec | Convert N-ary number string to decimal number |
bin2dec | Convert binary number string to decimal number |
dec2base | Convert decimal number to N-ary number in a string |
dec2bin | Convert decimal to binary number in a string |
dec2hex | Convert decimal numbers in a string to hexadecimal numbers |
hex2dec | Convert hexadecimal number string to decimal number |
hex2num | Convert hexadecimal number string to double precision number |
num2hex | Convert single and double precision to IEEE hexadecimal string |
cell2mat | Convert cell array to numeric array |
cell2struct | Convert cell array to structure array |
cellstr | Create a cell array of strings from a character array |
mat2cell | Convert array to cell array with possibly different cell sizes |
num2cell | Convert array to cell array of uniform size |
struct2cell | Convert structure to cell array |
MATLAB provides various functions to identify the data type of variables.
The following table provides the function to determine the data type of variables-
Function | Function |
---|---|
is | Detect status |
isa | Determine if the input is an object of a specified class |
iscell | Determine if the input is a cell array |
iscellstr | Determine if the input is a cell array of strings |
ischar | Determine if the item is a character array |
isfield | Determine if the input is a field of a structure array |
isfloat | Determine if the input is a floating-point array |
ishghandle | Used for handling graphic object handles |
isinteger | Determine if the input is an integer array |
isjava | Determine if the input is a Java object |
islogical | Determine if the input is a logical array |
isnumeric | Determine if the input is a numeric array |
isobject | Determine if the input is a MATLAB object |
isreal | Check if the input is a real array |
isscalar | Determine if the input is a scalar |
isstr | Determine if the input is a character array |
isstruct | Determine if the input is a structure array |
isvector | Determine if the input is a vector |
class | Determine the class of the object |
validateattributes | Check the validity of the array |
whos | List variables in the workspace, including size and type |
Create a script file using 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 running the file, it will produce the following results-
x = 3 ans = 0 ans = 1 ans = 1 ans = 1 ans = 1 x = 23.540 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