Updated March 4, 2023
Introduction of Display Text in Matlab
To display a text in MATLAB, we use ‘disp function’ which displays the text or value stored in a variable without actually printing the name of the variable. Another way used in MATLAB is to type the name of the variable in the command window after the variable is assigned a value; doing this will display the name of the variable also before its value. ‘Disp function’ can be used to display both numeric and string values stored in a variable.
Syntax:
disp (A)
Description:
- disp (A) will display the value of input variable A without printing the name of the variable
- For an empty input array, A, disp will return a blank screen i.e. nothing is displayed on the output screen
Let us now understand the code of ‘disp function’ in MATLAB with the help of various examples:
Examples of Matlab Display Text
Following are the examples are given below:
Example #1
In this example, we will learn how to display a simple array in MATLAB using disp function.
Below are the steps to be followed:
- Initialize the array whose elements we want to display
- Pass the array as a parameter to the disp function
Code:
A = [15 20 -3 4 -12 0 3 6]
disp(A)
Input:
A = [15 20 -3 4 -12 0 3 6]
disp(A)
Output:
As we can see in the output, the elements of array A are displayed using disp function. Please note that only the values of A are displayed, and not the variable ‘A’ itself.
Example #2
In this example, we will learn how to display a simple text in MATLAB using disp function.
Below are the steps to be followed:
- Initialize the string which we want to display
- Pass the string as a parameter to the disp function
Code:
A = 'Let us learn how to display text in MATLAB'
disp(A)
Input
A = 'Let us learn how to display text in MATLAB'
disp(A)
Output:
As we can see in the output, the input string is displayed using the disp function. Please note that only the text of A is displayed, and not the variable ‘A’ itself. In the above 2 examples, we used the ‘disp’ function to either display numeric values or text. Next, we will learn how to display values stored in multiple variables.
Example #3
In this example, we will learn how to use the disp function when we have multiple variables.
Below are the steps to be followed:
- Initialize the variables to be displayed
- Create a new variable with the above two variables and with any text required in between the variables
- Pass the variables as parameters to the disp function
Code:
Country = 'India'
Rank = 5
D = [ Country, ' is ranked ' , num2str(Rank), 'th in terms of nominal GDP' ]
disp(D)
Input:
Country = 'India'
Rank = 5
D = [ Country, ' is ranked ' , num2str(Rank), 'th in terms of nominal GDP' ]
disp(D)
Output:
As we can see, the output has values that are derived from multiple variables.
Example #4
Let us take another example where we will use disp function to get output displayed out of multiple variables. For this example, we will be combining 3 variables
Below are the steps to be followed:
- Initialize the variables to be displayed
- Create a new variable with the above two variables and with any text required in between the variables
- Pass the variables as parameters to the disp function
Code:
Country = 'India'
Rank1 = 5
Rank2 = 3
D = [ Country, ' is ranked ' , num2str(Rank1), 'th in terms of nominal GDP and ', num2str(Rank2), 'rd in terms of PPP ']
disp(D)
Input:
Country = 'India'
Rank1 = 5
Rank2 = 3
D = [ Country, ' is ranked ' , num2str(Rank1), 'th in terms of nominal GDP and ',
num2str(Rank2), 'rd in terms of PPP ']
disp(D)
Output:
As we can see, the output has values that are derived from multiple variables.
Conclusion
- To display text or numeric values in MATLAB, we use disp function
- Disp function helps us to get the output displayed without getting the name of the variable
- Disp function can be used both for a single variable and multiple variables
Recommended Articles
This is a guide to Matlab Display Text. Here we also discuss the syntax and parameters of Matlab display text along with different examples and its code implementation. You may also have a look at the following articles to learn more –