Variables in MATLAB – Definition, Characteristics, and Types


MATLAB is a high-level programming language that supports different kinds of fundamental elements. In MATLAB, there is a fundamental element called variable which is used to store data. A variable has a unique name referred to as identifier. A value is stored in the variable that can be utilized through the variable name. In this article, we will study about basics of variables and their characteristics. Also, we will explore different types of variables and their declaration with the help of examples.

What is a Variable in MATLAB?

In MATLAB, a variable is one of the programming elements that is used to store and represent data or information. Hence, a variable act as a storage space and has some specific memory associated with it. In this memory space, the values are stored that has to be assigned to the variable.

The MATLAB workspace provides facilities to create, reuse and manage the variables.

The most important thing that we have to remember about variables in MATLAB is that a MATLAB variable is interpreted as an array or matrix, irrespective of its data type.

In MATLAB, a variable can be used to store various types of data like text, numbers, arrays, structures, etc.

Components of Variable in MATLAB

The following are some main components of a variable in MATLAB βˆ’

  • Variable Name βˆ’ Each variable has a unique name associated with it. It is basically the identifier for the variable through which the variable is used in the MATLAB code.

  • Value βˆ’ A MATLAB variable can store different kinds of values like numbers, text, arrays, etc. It is the data stored in the memory space allocated to the variable.

  • Data Type βˆ’ MATLAB variable has a dynamic data type. Which means, MATLAB assigns a specific data type to the variable depends on the value stored in it.

  • Scope of Variable βˆ’ In MATLAB, a variable has a scope withing the workspace or a function where it is defined. Hence, a MATLAB variable is accessible within the MATLAB workspace or the function. However, we can also explicitly declare a variable as global variable.

Rules of Variable Naming in MATLAB

While assigning a name to a MATLAB variable, the following rules are to be followed, otherwise, it will cause an error in the code.

  • MATLAB variable names are case-sensitive. For example, β€œName” and β€œname” are two different variables.

  • In MATLAB, a variable name must not be a MATLAB keyword. For example, we cannot use β€œfor” as a variable name.

  • The MATLAB variable name must start with a letter, and it can have letters, digits, or underscores.

  • In MATLAB, we can use a variable name of a maximum of 63 characters in length.

Hence, we must follow these four rules to declare a valid variable in MATLAB.

How to Declare a Variable in MATLAB?

In MATLAB, we can declare a variable by giving a name and assigning a value to it. The basic syntax for variable declaration in MATLAB programming is as follows βˆ’

variable_name = value;

Hence, declaring a variable in MATLAB is a three-step process i.e.,

  • Selecting a variable name as per the MATLAB variable naming rules.

  • Assigning a value to the variable using an equal sign β€œ=”.

  • End with a semicolon β€œ;”. However, this is an optional step. If we do not use a semi-colon to end the variable, MATLAB will display the value of the variable in the result window.

Variable Declaration Example in MATLAB

The following example demonstrates how to declare a variable in MATLAB.

Example

% Declaring a variable with a numeric value 
age = 57;

% Declaring a variable with text value
name = 'Tutorials Point';

% Declaring a variable with a matrix 
mat = [10, 20; 30, 40];

After declaring a variable in the MATLAB workspace, we can use it in our MATLAB codes.

Types of Variables in MATLAB

In MATLAB, the variables have dynamic data types, which means the data type is automatically assigned to the variable depending on the value assigned to it. Depending on the data types, we can create various types of variables in MATLAB. Some commonly used types of MATLAB variables are explained below.

Numeric Type Variables

MATLAB supports the following three-types of numeric variables.

Integer

MATLAB supports both signed and unsigned integer type variables. We can use different sizes of integer type variables depending on the size of the data.

Example

% Declaring an 8-bit signed integer
Var1 = int8(40);

% Declaring a 16-bit signed integer
Var2 = int16(70);

% Declaring a 32-bit signed integer
Var3 = int32(30);

% Declaring a 64-bit signed integer
Var4 = int64(70);

% Declaring an 8-bit unsigned integer
Var5 = uint8(40);

% Declaring a 16-bit unsigned integer
Var6 = uint16(70);

% Declaring a 32-bit unsigned integer
Var7 = uint32(30);

% Declaring a 64-bit unsigned integer
Var4 = uint64(70);

Single

It is used to create a variable to store single-precision floating-point numbers. Due to low precision, it consumes less memory space.

Example

% Declaring a single type variable
X = single(22.7);

Double

It is used to create a variable to store double-precision floating-point numbers. In MATLAB, it is the default numeric data type for floating point numbers.

Example

% Declaring a double type variable
X = double(2.45889874637);

Character Type Variables

Character type variables are used to store individual characters or a string of characters. Its value is specified by enclosing within single quotes.

Example

% Declaring a character type variable
X = 'Tutorials Point India Pvt Ltd.';

Complex Type Variables

The complex type variables are used to store complex numbers with real and imaginary parts.

Example

X = complex(10, 17);
This is equivalent to 5 + 17i.

Logical Type Variables

The logical type variables are used to store Boolean values β€œtrue” or β€œfalse” in MATLAB.

In the case of this type of variable, a non-zero value is treated as β€œtrue (logical 1)” and a zero is treated as β€œfalse (logical 0)”.

Example

A = [10, 0, 15, 4, 0, 5];
B = logical(A);
The output will be,
B = 1	0	1	1	0	1

Cell Array

The cell array is a variable that is used to store data of different data types. Hence, the cell array allows us to create a variable capable of storing heterogeneous data. In MATLAB, we create a cell array by using β€œ{}”.

Example

X = {'Courses', 'Tutorials', 400, [12, 5, 90]};

Structures

In MATLAB, structures are used to combine related data together into a single data structure.

Example

x.id = 1001;
x.name = 'Manish';
x.age = 24;

Function Handles

In MATLAB, a function handle is a variable used to reference a function.

Example

x = @cos;

Here, the β€œx” is a function handle for the function β€œcos”.

Conclusion

In conclusion, MATLAB is a programming language that supports different types of variables. Where, a variable is a programming element used to store a specific type of data. It basically acts as a name assigned to a memory location and stores data and information.

MATLAB has a unique feature that it automatically assigns a data type to a variable depending on the value stored in it. However, it also allows to specify a specific data type to a variable by using a function like single(), double(), complex(), etc.

In this detailed article on variables in MATLAB, I have explained the basics of MATLAB variables and different types of variables in MATLAB along their declaration syntax.

Updated on: 13-Nov-2023

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements