In this blog, we will walk through how to display variable values in MATLAB using the disp() function.
Display the Value of Variable Using MATLAB disp() Function
A disp() is a built-in function in MATLAB used to display different values of different data types on the screen without their variable names. This function can also print a vector or an array. This function accepts the variable name as an input and displays the value or values stored in the variable on the screen without displaying the variable name.
Syntax
The disp() function in MATLAB has the following syntax:
Here:
The function disp(var) displays the value stored in the variable var without printing the variable name. If the variable does not contain any value or contains an empty array, the disp() function will not display anything.
Examples
Consider some examples to understand the working of the disp() function to print the variable’s value on the screen.
Example 1: Use disp() Function to Display Values
The given MATLAB code uses the disp() function for displaying the values contained by the variables x and str on the screen.
disp(x)
str = 'Welcome to linuxhint';
disp(str)
Example 2: Use disp() Function to Print the Vector, Identity Matrix, and Array
In this example, we print the vector x, the identity matrix A, and the array arr of random numbers on the screen using the disp() function.
disp(x)
A = eye(2);
disp(A)
arr = rand(2, 3, 2);
disp(arr)
Conclusion
The disp() is a MATLAB function that enables you to print the value of a variable on the screen. This function also prints multiple values stored in a vector, a matrix, or an array. The function accepts the variable name as an input and displays a value or values contained by that variable on the screen without displaying the variable name. In this guide, we have learned how to print the value of a variable in MATLAB using the disp() function. By using the disp() function in MATLAB, you can easily examine and verify the values stored in your variables during the program execution.