The sprintf function works precisely like the fprintf function, but rather than printing it generates a string. Here are some illustrations in which the output is not suppressed and hence the value of the string variable is shown:
>> sent1 = sprintf('The value of pi is %.2f', pi)
The value of pi is 3.14
>> sent2 = sprintf('Some numbers: %5d, %2d', 33, 6)
Some numbers: 33, 6
In the illustration below, on the other hand, the outcome of the assignment is suppressed; therefore the string is generated including an arbitrary integer and stored in the string variable. Then, few exclamation points are concatenated to that string.
>> phrase = sprintf('A random integer is %d', . . .
>> strcat(phrase, '!!!')
A random integer is 7!!!
All the conversion specifiers which can be used in the fprintf function can also be used in the sprintf function.