MATLAB Variables
This tutorial will discuss creating variables and storing data in variables in Matlab.
MATLAB Variables
Variables are used to store data in Matlab. When we create a variable, Matlab will allocate some memory to that variable to store data.
If we store data in a variable, we can use the variable name instead of the data.
For example, if we want to multiply and add two numbers, we can save them in two variables, and then we can multiply and add them using their name.
See the code below.
clc
a = 100;
b = 50;
mul = a*b
add = a+b
Output:
mul =
5000
add =
150
If we use a semicolon after storing value in a variable, Matlab will not show the value in the command window. In Matlab, we can write code in a script file as well as in the command window.
The above code is written in a script file. In the output, you can see that only the variables mul
and add
are shown on the command window because they don’t contain semicolons at the end. You can also see the variables and their values in the workspace window.
The clc
command at the start of the code is used to clear the command window. We can use the whos
command to check the variable name, size, bytes, and class or data type of the variables.
For example, let’s use the whos
command in the above code. See the code below.
a = 100;
b = 50;
mul = a*b;
add = a+b;
whos
Output:
Name Size Bytes Class Attributes
a 1x1 8 double
add 1x1 8 double
b 1x1 8 double
mul 1x1 8 double
In the output, all the variables have the same size and class. The variables stored inside the workspace will stay there unless we close Matlab or use the clear
command to clear them from the workspace.
We can store data of all kinds of class or data types in a variable in Matlab, like characters using single quotation marks, strings using double quotation marks, cell array using curly brackets, vectors using square brackets, and matrices using square brackets.
Let’s create variables with different data types. See the code below.
a = {1,2};
b = [1 2];
c = 'Char';
d = "String";
e = {'char',"string",5};
whos
Output:
Name Size Bytes Class Attributes
a 1x2 224 cell
b 1x2 16 double
c 1x4 8 char
d 1x1 150 string
e 1x3 478 cell
We can store different data types or classes variables inside a cell array.
A variable name should start with a letter containing numbers and underscore. It should not contain full stop, arithmetic symbols, and space. If you want to write multiple strings in a variable name, you can use underscore to separate them from each other.
If we pass values separated by a space inside square brackets, it will create a row vector. If we pass the values separated by a semicolon, Matlab will create a column vector.
In the same way, we can create matrices in Matlab, write the values separated by a space to create a row and then add a semicolon to jump to the second row.
For example, let’s create a row vector, column vector, and a matrix in Matlab. See the code below.
a = [1 2 5]
b = [1;2;5]
c = [1 2; 3 5]
whos
Output:
a =
1 2 5
b =
1
2
5
c =
1 2
3 5
Name Size Bytes Class Attributes
a 1x3 24 double
b 3x1 24 double
c 2x2 32 double
You can check the size of the variable to know if it’s a row vector, column vector, or matrix.
A value should be stored inside a variable before it is used. If we don’t assign an expression value to a variable, Matlab will assign the value to a variable, which can be used later.
There are many reserved keywords in Matlab which we cannot use as a variable name. To check the reserved variables in Matlab, we can use the iskeyword
command, which will list all the reserved keywords.
There are predefined expressions in Matlab which we can use in our code, like pi
.
We can use the colon operator to create a large array of numbers. Creating an array of numbers requires a lot of time, like creating an array of integers from 1 to 100. In this case, we can use the colon operator to create the array in no time.
For example, let’s create an array from 1 to 100. See the code below.
a = 0:1:100;
whos
Output:
Name Size Bytes Class Attributes
a 1x101 808 double
To create the array, we have to write the initial value, increment, and final value separated by the colon operator.