Matlab

How to Format Data into String or Character Vector with MATLAB’s sprintf?

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 = sprintf(formatSpec,A1,...,An)
[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.

v = rand(1,10);
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

formatSpec = "The current time is: %d:%d %s";
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.

formatSpec = "The two integer number: %d+%d %s";
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.

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.