## Introduction to Matlab Variables

The variables, created in MATLAB code are handled by its workspace and used to define memory locations and store values assigned to each respective variable. Matlab workspace supports creating new variables as well as reusing of existing variables in command execution. In the Matlab environment, each variable is treated as a matrix or an array irrespective of their data types.

### Declaring variables in Matlab

Matlab does not need variables to be declared explicitly. When the user specifies the variable name followed by equation and data, a variable with the given variable name is instantiated holding the assigned data within.

The data can be assigned to a variable in ways:

#### 1. Assigning a constant value to the variable

**Code:**

`var1 = 15.41`

var2 = [10 22 43; 40 45 63; 27 48 19]

**Output**:

#### 2. Assigning an expression to a variable

**Code:**

`var1 = sqrt(16)`

var2=sin(1)

**Output**:

#### 3. Initializing variables in Matlab

Matlab variables are initialized with the initial value assigned to it. Matlab variables support both empty and non-empty data as initial values.

**Code:**

```
var1 = []
var2=10
```

**Output**:

### How do Variables work in Matlab?

Once a variable is created from code, Matlab adds the variables to the workspace.

4.5 (4,859 ratings)

View Course

**Code:**

`var1="I am variable";`

I = [1 2 3; 4 5 6; 7 8 9];

**Output:**

**Explanation:** The content of the variable can be edited using command or can be edited from the variable editor. Apart from editing, the other operations such as deleting, duplicating, copying, resizing, or reshaping the variable can be done in the workspace.

Multiple variables can be declared in a single command line from the Matlab command window.

**Code:**

`var1=50;var2=60;var3=70;`

**Output**:

### Types of variables in Matlab

Various data types are supported by Matlab variables, such as:

#### 1. Char

The array of characters is supported by Matlab variables.

**Code:**

`char_var = "I am a char type variable";`

char_var_arr = ["charset1","charset2","charset3"];

**Output:**

#### 2. Complex

The complex variables i.e. in the form of “real +i*imaginary” are supported by Matlab variables.

**a. Creating a complex number:**

**Code:**

`complex_var = complex(5,10);`

**Output**:

**b. Creating an array of complex numbers:**

**Code:**

`real = uint8([11;25;23;40]);`

imgainary = uint8([21;25;27;47]);

complex_arr = complex(real,imgainary)

**Output**:

#### 3. Double

In Matlab, the default numeric data type or class is ‘double’, which provides high precision for most of the computational tasks. Numeric variables are captured in the form of 64-bit (8-byte) double-precision floating-point value automatically.

**Code:**

`var_name = 100;`

xtype = class(var_name)

**Output:**

It supports variable datatype conversion to double.

**Code:**

`other_var = true`

other_var_double = double(other_var);

other_var_newtype = class(other_var_double)

**Output**:

#### 4. Signed integer

Matlab supports variable arrays that of the singed integer data type. The variables can be defined as 8/16/32/64-bit signed integer depending on the size of the data.

**Code:**

`var_int8 = int8(50) ; var_int16 = int16(50); var_int32 = int32(50); var_int64 = int64(50);`

**Output:**

#### 5. Logical

Logical value can be used in Matlab programming in the form of a variable. But Complex values and NaNs are not supported for logical value conversion and throws conversion error.

Any non-zero element gets converted as logical 1 i.e. Boolean true and 0 gets converted as logical 0 i.e. Boolean false.

**Code:**

`A = [10 -23 0;35 42 71; -28 0 36];`

L = logical(A)

**Output:**

#### 6. Single

Matlab supports Single-precision variable declaration and those get stored as ‘single’ datatype 4-byte (32-bit) floating-point values.

**Code:**

`single_var= single(100)`

vartype = class(single_var)

**Output:**

#### 7. Structure

A structure array can be defined as a data type that allows grouping related data using data containers which are termed as fields and can be used as a user-defined data type. A field can contain data of any Matlab supported data type of class.

The data of a field can be accessed using dot notation form as given as ‘structName.fieldName’.

Syntax |
Description |

s = struct | create structure in the form of a scalar with size 1X1, with no fields. |

s = struct(field,value) | creates a structure in the form of an array with the field and value defined for the structure. |

s=struct(field1,value1,…,fieldN,valueN) | creates a structure in the form of an array with multiple fields (field1, field2,…, fieldN) and values(value1, value2,…..,valueN), defined for the structure. |

s = struct([]) | used to create a null structure(0-by-0) with no fields. |

s = struct(obj) | used to create a scalar structure consisting of field names and values corresponding to the properties of ‘obj’. |

**Code:**

`struct_var = struct`

**Output:**

**Code:**

`field_name = 'field_name';`

values = {'A string';

[15, 25, 35];

magic(4)};

s = struct(field_name,values)

s.field_name

**Output**:

**Code:**

`field1 = 'field1'; value1 = zeros(1,15);`

field2 = 'field2'; value2 = {'str1', 'str2'};

field3 = 'field3'; value3 = {pi/3, pi.^3};

field4 = 'field4'; value4 = {'text'};

s = struct(field1,value1,field2,value2,field3,value3,field4,value4);

s.field1

s.field2

s.field3

s.field4

**Output:**

**Code:**

`s=struct([])`

**Output**:

#### 8. Unsigned Integer

Matlab supports variable arrays that of the unsigned integer data type. The variables can be defined as 8/16/32/64-bit unsigned integer depending on the size of the data to be stored in the variable.

**Code:**

`var_int8 = uint8(50) ; var_int16 = uint16(50); var_int32 = uint32(50); var_int64 = uint64(50);`

**Output:**

#### 9. Fixed point data type

Matlab SIMULINK blocks allow users to create models that use fixed-point datatypenumerical to represent parameter values and signals. The application of fixed-point data reduces memory consumption and improves the speed of code that is generated from a SIMULINK model.

**Code:**

`var1 = fi(pi)`

**Output**:

**Note:**When the return value from an expression is not assigned to any variableexplicitly, a default variable ‘ans’ gets defined by the system and the return value of the expressions gets assigned to it. This default variable ‘ans’ is reusable throughout the code. The elements or their subsets from a multidimensional array and tall arrays are not editable in the Variables editor. In the Workspace browser or the Variables editor, double quotes need to be used surrounding the string value while editing any string type variable.

If an empty variable is created or variable gets empty value during run time, the variable will be assigned to a default value based on the data type such as:

For numeric array variable: ‘0’

For cell and structure array variable: ‘[]’

For categorical variable: ‘<undefined>’

### Recommended Articles

This is a guide to Matlab Variables. Here we discuss an introduction to Matlab Variables, how does it work, along with types and examples. You can also go through our other related articles to learn more –