Matlab

How to Use disp() Function with Variables in MATLAB?

MATLAB is a helpful programming tool that has a vast library of built-in functions to perform many tasks and display the results on the screen. It automatically prints the value of a variable on the screen without using any built-in function. But, when we use the semicolon (;) in front of the variable’s value, the output will be hidden. To resolve this issue, MATLAB uses the disp() built-in function to display the variable’s value on the screen.

This blog will explore how to use the disp() function with variables in MATLAB.

What is the disp() Function in MATLAB?

The disp() is a built-in function in MATLAB that allows us to display the value of a variable without printing the variable name on the screen. This function can print a single variable having any data type. This function can print a scalar, a vector, a matrix, or an array on the screen. It cannot print a variable with a string or multiple variables at a time.

Syntax

The disp() function’s syntax is given below:

disp(name of a variable) or disp(‘text as string’)

How to Use the disp() Function with Variables in MATLAB?

We can make it possible to print values of multiple variables at a time in MATLAB using the disp() function by following the given steps:

Step 1: Declare two or more variables having any type of data.

Step 2: Define an array inside the disp() function that contains the defined variables as its entries.

Step 3: The disp() function displays the values of all variables in a single execution on the screen.

Example 1: How to Use disp() Function with Variables Having Numeric Values in MATLAB?

In this example, we use the disp() function to print values of two variables x and y using the num2str() function. The num2str() function converts the numeric value into the string.

x =10;
y = pi;
disp(['Value of variable x is ', num2str(x), ' and the value of variable y is ', num2str(y)])

Example 2: How to Use disp() Function with Variables Containing Text in MATLAB?

The given example uses the disp() function to print values of three variables str1, str2, and str3 with text stored in each variable.

str1= 'Welcome';
str2 = ' to';
str3 = ' linuxhint';
disp([str1,str2,str3])

Conclusion

By default, the disp() function does not allow us to print the values of multiple variables in a single execution. However, it can be done by defining an array containing all variables inside the disp() function. Adopting this method, we can easily print the values of multiple variables having any data type using the disp() function. This guide has explored the method to use the disp() function with variables in MATLAB by providing some examples that will help understand the basics of this function.

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.