MATLAB variables
This tutorial will discuss creating variables in Matlab and storing data in variables.
MATLAB variables
Variables are used to store data in Matlab. When we create a variable, Matlab allocates some memory for that variable to store the 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 use their names to multiply and add.
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 a value into a variable, Matlab will not display the value in the command window. In Matlab, we can write code in script files and 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 displayed in the command window because they do not contain a semicolon at the end. You can also view the variables and their values in the workspace window.
The command at the beginning of the code clc
is used to clear the command window. We can use whos
the command to check the name, size, bytes, and class or data type of a variable.
For example, let us use the command in the above code whos
. 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 variables have the same size and category. clear
Variables stored in the workspace remain there unless we close Matlab or clear them from the workspace using the command.
We can store data of various classes or data types in variables in Matlab such as characters using single quotes, strings using double quotes, cell arrays using curly brackets, vectors using square brackets, and matrices using square brackets.
Let us 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 class variables in cell arrays.
A variable name should start with a letter including numbers and underscores. It should not contain periods, arithmetic symbols, and spaces. If you want to write multiple strings in one variable name, you can separate them from each other using underscores.
If we pass the values separated by spaces inside the square brackets, it will create a row vector. If we pass the values separated by semicolons, Matlab will create a column vector.
In the same way, we can create a matrix in Matlab by writing values separated by spaces to create a row, and then adding a semicolon to jump to the second row.
For example, let us 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 a variable to know whether it is a row vector, column vector, or matrix.
Values should be stored in variables before using them. If we do not assign expression values to variables, Matlab will assign the values to the variables and we can use them later.
There are many reserved keywords in Matlab that we cannot use as variable names. To check the reserved variables in Matlab, we can use iskeyword
the command which will list all the reserved keywords.
There are some predefined expressions in Matlab that 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 takes 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 instantly.
For example, let us 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 an array, we have to write the initial value, incremental and final value separated by the colon operator.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Commenting multiple lines in MATLAB
Publish Date:2025/04/18 Views:115 Category:MATLAB
-
This tutorial discusses how to comment multiple lines of code in MATLAB using the comment block method and the MATLAB Editor. Use comment blocks in MATLAB to comment multiple lines of code To comment one or two lines of code, we can % do it
Use mean() function in Matlab to get the average value of an array
Publish Date:2025/04/18 Views:145 Category:MATLAB
-
This tutorial will discuss finding the mean or average of an array using the function in MATLAB mean() . Use the MATLAB mean() function to find the average of an array To find the mean of an array, we can use Matlab's inbuilt function mean(
Find the index of a value in an array in Matlab
Publish Date:2025/04/18 Views:189 Category:MATLAB
-
This tutorial discusses find() finding the index of a value in an array using the function in MATLAB. Use the function in MATLAB find() to find the index of a value in an array In an array, elements are placed at certain indices starting fr
Sum of array elements in MATLAB
Publish Date:2025/04/18 Views:153 Category:MATLAB
-
This tutorial will discuss the use of function in Matlab sum() to find the sum of all the elements in an array. Use the MATLAB sum() function to get the sum of array elements. To get the sum of each element in an array, we can use the built
Plotting Slope Fields in MATLAB
Publish Date:2025/04/18 Views:196 Category:MATLAB
-
The ODE consists of equations involving functions and their derivatives 常微分方程 . We use 斜率 fields to illustrate the concept of our 微分 equations. We also call slope fields fields direction . Use the function in MATLAB slope_
Plotting the frequency distribution of data in MATLAB
Publish Date:2025/04/18 Views:140 Category:MATLAB
-
We will study different ways to plot frequency distribution curve of data in MATLAB. We will use different sample codes and relevant outputs to clear your concepts and provide you complete insights using MATLAB. Note that MATLAB allows user
Creating a new graph in Matlab
Publish Date:2025/04/18 Views:192 Category:MATLAB
-
In this tutorial, we will discuss how to figure() create a new graph using the function in MATLAB. figure() Create a new figure using the MATLAB function If you want to plot data on multiple graphs, you can use figure() the function to crea
MATLAB Maximize Graph
Publish Date:2025/04/18 Views:101 Category:MATLAB
-
In this tutorial, we will discuss how to use figure() the function in MATLAB to maximize a graph. figure() Maximize graphs using the function in MATLAB If you want to maximize a graph, you can use figure() the maximize function. To maximize
Changing the Properties of Graphics in MATLAB
Publish Date:2025/04/18 Views:129 Category:MATLAB
-
We will study different ways to change the size, resolution, and background color of the desired figure in MATLAB. We will use different sample codes and relevant output to clear your concepts and give you a comprehensive understanding of t