Matlab

How to Use fprintf with Mixture of Strings and Numbers in MATLAB

The fprintf function is used in MATLAB for saving the output to a file or displaying the results (text and data) on the screen. It enables you to specify the layout, alignment, and data types to be printed, making it an effective function for generating well-formatted output.

How to Use fprintf Function in MATLAB?

The fprintf function in MATLAB is commonly used to print strings, numbers, or a mixture of both in a formatted manner on the screen or other output devices. It allows you to specify the layout and format of the output, making it a versatile tool for displaying data.

The simple syntax of the fprintf function in MATLAB is as follows:

fprintf(formatSpec,A1,A2,...,An)

The format argument defines the format string that specifies the layout and format of the output. The A1, A2, … An represent the values you want to print. By utilizing appropriate format specifiers within the format string, you can control how different data types are displayed and arrange them in the desired order.

Format Specifiers

Format specifiers are placeholders within the format string that indicates where and how the data should be printed; here are some commonly used format specifiers for mixing strings and numbers:

  • %s: Represents a string value.
  • %d or %i: Represents a decimal (integer) value.
  • %f: Represents a floating-point value.
  • %e or %E: Represents a floating-point value in scientific notation.
  • %g or %G: Represents a floating-point value in either decimal or scientific notation.

Example 1 – Printing Integers Using MATLAB fprinf

This simple example demonstrates how to use the fprintf function in MATLAB to display strings and numbers. This example simply calculates and displays the sum of two integer values using the fprintf function.

x= 4;

y= 8;

fprintf("Sum of %d and %d is: %d", x, y, x+y)

Example 2 – Printing Strings Using MATLAB fprinf

In this example, the given MATLAB code is printing the string with %s specifier:

name = "sam";

fprintf("The name of person is %s",name);

The output is:

Example 3 – Printing the Mixture of Strings and Integers Using MATLAB fprinf

To print the mixture of strings and integers both %d and %s specifiers will be used in fprintf function:

name = "sam";

age = 25;

fprintf("The name of person is %s and he is %d years old",name, age);

The output will be:

Conclusion


The fprintf function in MATLAB is an effective tool for formatting and printing data. By mixing strings and numbers within the format string, you can create well-structured output that effectively conveys information. Understanding the format specifiers and utilizing them in the fprintf function will help you control the output appearance and layout.

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.