Matlab

How to Display Output Variable with Text in MATLAB?

MATLAB is a beneficial programming language that includes a vast library of built-in functions to solve many problems and display the results on the screen. It automatically prints the solutions on the screen without any function. But sometimes, we are required to print the output with some text to make our solution understandable.

This blog will teach you how to display the output variable with text in MATLAB.

Why Do We Need to Display Output Variable with Text in MATLAB?

Displaying the output variable with text in MATLAB can assist you to make your code more readable as well as understandable. It also provides context to the output variable as well as adds formatting to it. You can also print error messages in your code to help you identify the source of the error.

How to Print Output Variable with Text in MATLAB?

To display output variables with text in MATLAB, you can use:

1: How to Display Output Variable with Text Using disp() Function in MATLAB?

The disp() is a built-in MATLAB function that enables us to print the value of a variable without displaying the variable name. This function can print just a variable value or some text at a time and cannot print both at one time. However, we will use it to print the variable with the text by doing some modifications.

Syntax

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

disp(variable’s name) or disp('string')

Example

This example describes how to implement the disp() function to display the output variable with text in MATLAB.

x =10;
y = 12;
z = x+y;
X = ['Sum of',num2str(x),' and ', num2str(y), ' is ' num2str(z)];
disp(X)

In the above code, the variable X is a string that contains the text ‘Sum of’, the value of x, y and z converted to string using the num2str() function. This argument is then passed through the disp() function that displays the output variables with text in the Command window.

2: How to Display Output Variable with Text Using fprintf() Function in MATLAB?

The fprintf() is a built-in function in MATLAB that is used especially for displaying the value of a variable with text or string on the screen. This function is also used for saving the output value in a file. You can use this function to display the output variable with text and format it accordingly using different format specifiers.

Syntax

The fprintf() function follows the given simple syntax:

fprintf('text as string',variable_name)

Example

In this example, we use the fprintf() function for displaying the output variable with text in MATLAB.

x =10;
y = 12;
z = x+y;
fprintf('Sum of %d and %d is %d', x,y,z);

The above code uses the format specifiers inside the fprintf() function to display the value of the variable on the command window.

For more detail, visit here.

Conclusion

MATLAB has a huge library of built-in functions to perform various tasks. It facilitates us with built-in functions disp() and fprintf() to display different variables having different types of data. The disp() function displays a value of a variable or string at a time, but you can modify it to print out variables with text. The fprintf() function is straightforward and easy to use as it displays both variable’s value and string at a time using the format specifier and allows various formatting options. We have used both these functions in this guide to help you learn how to display output variables with text in MATLAB.

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.