Updated March 6, 2023
Introduction to Matlab disp
Disp function is used in MATLAB to display the output of any code without displaying the input variables. This function can be used in cases where our code is not very long or easy to understand, and there is no need of displaying the input variables.
For example, if we use the disp function to display a string ‘MATLAB Disp function’ stored in a variable ‘A’, our output will be ‘MATLAB Disp function’. However, if we do not use the disp function, our output will be A = ‘MATLAB Disp function’.
Syntax of the disp function:
disp (A)
Description of the disp function:
disp (A) function is used to display or print the value stored in the variable ‘A’ without printing the name of the variable
Examples of Matlab disp
Let us now understand the code to use the disp function in MATLAB.
Example #1
In this example, we will use the disp function to display elements of a vector. Below are the steps to be followed:
- Initialize the input vector
- Use the disp function to display the elements of the vector
Code:
A = [3 7 1 8 10]
[Initializing an input vector and storing it in a variable ‘A’]
disp (A)
[Using the disp function to display the value stored in the variable ‘A’]
This is how our input and output will look like in MATLAB:
Input:
Output:
As we can see in the output, we have obtained the value (i.e. elements in our case) stored inside the variable ‘A’ by using the disp function.
If we print the output without using the disp function, this is how it will look like:
Please note that in this case, even the variable name is displayed in the output.
Example #2
In this example, we will use the disp function to display elements of a matrix. Below are the steps to be followed:
- Initialize the input matrix
- Use the disp function to display the elements of the matrix
Code:
A = [3 3 1; 8 0 4; 1 4 9]
[Initializing a 3 x 3 input matrix and storing it in a variable ‘A’]
disp (A)
[Using the disp function to display the value stored in the variable ‘A’]
This is how our input and output will look like in MATLAB:
Input:
Output:
As we can see in the output, we have obtained the value (i.e. elements of the matrix in this example) stored inside the variable ‘A’ by using the disp function.
If we print the output without using the disp function, this is how it will look like:
Please note that in this case, even the variable name is displayed in the output.
Example #3
In this example, we will use the disp function to display a string. Below are the steps to be followed:
- Initialize the input string
- Use the disp function to display the string
Code:
A = "Let us learn MATLAB disp function"
[Initializing a string and storing it in a variable ‘A’]
disp (A)
[Using the disp function to display the value stored in the variable ‘A’]
This is how our input and output will look like in MATLAB:
Input:
Output:
As we can see in the output, we have obtained the value (i.e. a string of words in this example) stored inside the variable ‘A’ by using the disp function.
If we print the output without using the disp function, this is how it will look like:
Please note that in this case, even the variable name is displayed in the output.
Example #4
In this example, we will use the disp function to display the output of a mathematical expression. Below are the steps to be followed:
- Initialize the variables involved in the expression
- Use the disp function to display the output
Code:
X = 5
[Initializing the 1st input variable]
Y = 10
[Initializing the 2nd input variable]
Z = 25
[Initializing the 3rd input variable]
A = X * Z / Y
[Mathematical expression to be solved]
disp (A)
[Using the disp function to display the value stored in the variable ‘A’]
This is how our input and output will look like in MATLAB:
Input:
Output:
As we can see in the output, we have obtained the value of our output stored inside the variable ‘A’ by using the disp function.
If we print the output without using the disp function, this is how it will look like:
Please note that in this case, even the variable name is displayed in the output.
Conclusion
- We use the disp function to display the value stored inside a variable without printing the name of the variable.
- It can be used if we want our output to look neat, without any other information except the actual output.
Recommended Articles
This is a guide to Matlab disp. Here we discuss the Examples of Matlab disp along with the code to use the disp function in MATLAB. You may also have a look at the following articles to learn more –