When we need to print some data on the screen, we have to format it before displaying it. This can be easily performed using the **sprintf()** function. This function works in the same way as **fprintf() **but there is a difference in the working of these two functions. The** fprintf()** function writes data into a text file while **sprintf()** writes data into a data string.

Follow this guide to understand the functionality of the **sprintf()** function with different syntaxes and examples.

## How to Format Data into String or Character Array in MATLAB?

The **sprintf() **is a built-in MATLAB function that allows us to format data in any format using various specifiers like **%e, %s, %d, %f,** and so on. This function accepts a format specifier and an array of characters as input arguments and returns a formatted string.

## Syntax

We can use the **sprintf()** function in MATLAB through the following syntaxes.

[str,errmsg] = sprintf(formatSpec,A1,...,An)

str = sprintf(literalText)

Here:

The function **str = sprintf(formatSpec,A1,…,An)** is responsible for formatting data in the given array **A1,A2,…, An** using the specified format specifier **formatSpec **and provides the resulting text string str.

This function formats data in **A1, A2,…, An** in column order. If **formatSpec **represents a string, then str will behave like a string otherwise it will represent a vector of characters.

The function **[str,errmsg] = sprintf(formatSpec,A1,…,An) **is responsible for displaying an error message in the form of character vectors when the operation is not successful otherwise **errmsg** will be empty.

The function **str = sprintf(literalText) **is responsible for translating escape sequences into literal text such as \n and \t. This function unaltered all the characters except the escape characters.

## Example 1: How to Format Floating Point Numbers in MATLAB?

This MATLAB code formats the given floating-point numbers in the **%s** format using the **sprintf()** function.

A = 1./exp(v);

str_e = sprintf('%s',A)

## Example 2: How to Specify Formatted Text as a String Array in MATLAB?

In this example, we pass the argument **formatSpec** as a string rather than a character vector. After that, we display the current time stored in the variables A1, A2, and A3 using the **sprintf()** function

A1 = 02;

A2 = 10;

A3 = 'p.m.';

[str,errmsg] = sprintf(formatSpec,A1,A2,A3)

## Example 3: How to Specify Formatted Text as a String Array in MATLAB?

In this MATLAB code, we pass the argument **formatSpec** in the form of a string. After that, we display the values stored in the variables arr_1, arr_2, and arr_3 in the form of string using the **sprintf()** function.

arr_1 = 10;

arr_2 = 5;

arr_3 = "present here with + operator";

A = sprintf(formatSpec,arr_1,arr_2,arr_3)

## Conclusion

MATLABâ€™s **sprintf()** function enables us to format data using any format specifier. This function operates like the **fprintf()** function, but it writes data into a data string instead of writing the data to a text file. This guide has presented an easy and simple implementation of the **sprintf()** function in MATLAB with examples that help us grasp the understanding of the function.