Matlab

How to Display Value of Variable in MATLAB Using disp() Function?

Displaying the values of a variable in MATLAB is useful for understanding and debugging your code. It will help users while working with complex numbers or large datasets, allowing them to keep track of variable values to ensure accuracy and identify any errors. MATLAB provides a built-in disp() function that can display the value of a variable directly on the screen.

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:

disp(var)

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.

x = 10;
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.

x = 1:10;
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.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.