Importance of Format Specifier(%s,%d,%f)
You can combine formatting operators with popular conversion methods, such num2str and sprintf, to convert data to text and regulate its formatting. These operators regulate the nomenclature, alignment, significant digits, and other aspects of the data.
Importance of Format Specifier %s,%d,%f.

This article helps to understand what are the common data types in Matlab through a detailed explanation on the format specifiers with various examples.
What are Format Specifiers in Matlab?
You can combine formatting operators with popular conversion methods, such as num2str and sprintf, to convert data to text and regulate its formatting. These operators regulate the nomenclature, alignment, significant digits, and other aspects of the data. They are comparable to those employed by the C programming language's printf function. Formatted text is frequently used in output and display files.
What are the Variables used in Matlab?
The variables used in Matlab are broadly classified into two types:
- String
- Number
What is String?
- An array of characters forms the string.
- The character represents a letter.
- If the number is converted to a character, it cannot be used for arithmetic operations.
Example:
In the above example, a set of characters( skill-lync is an Ed-tech Company ) is defined in a variable ‘A’. Hence now the variable ‘A’ is a string.
Note: A string should always be defined inside the quotes.
What is a Number?
In Matlab, whatever numerical values are defined in a variable will be taken as a matrix.
- We can do arithmetic operations by using the variables which carry numbers.
Example:
The difference between the character and a number can be checked in the workspace,
These two are interconvertible, i.e. a string can be converted into a number or Vice Versa by using some inbuilt commands in Matlab.
For Example:
str2num converts a string into a numeric matrix. This command is used while file parsing, where you will read the text file, line by line and get useful data from the file.
num2str converts a numeric matrix into a character array. This command will be useful for labelling and titling plots with numeric values.
Printing the Output:
The main coding objective is to get useful information as the final output. Hence certain commands like fprintf, sprintf, etc will help to display the output properly. For these commands, you have to specify the data with a format specifier.
Note:
- fprintf command is used to write data to a text file and also to print the data in the command window.
Example:
On executing the above command, fopen creates a new text file, named ‘skill-lync.txt’ and the fprintf command writes the data to a text file, for loop is used to write the data for 10 iterations. Hence ten lines are printed in the text file, and \n helps to create a new line.
- sprintf command is used to format the data into a character variable, and we can use that character for printing output data or plotting purposes.
Example:
The data is formatted into a character variable ‘a’ on executing the above command.Check the workspace, you will find the formatted text with the value is stored in the variable ‘a’.
Note:
fprintf is used to write the data into a separate text file or it can be used to write in the command window, whereas sprintf is used to format the data(the character with a number) to a variable which can be used for printing or plotting purposes.
What is the Format Specifier?
Format specifier represents what the data format is. As we mentioned above, strings and numbers are the broad classifications of data types. Thus the format specifier helps to represent the data, whether a string (or) a number.
Let’s see the notation for the format specifier.
1. %s ⇒ denotes the character/string variable.
- ‘%s’ format specifier prints the character or string variable.
Example:
- Now, the variable ‘A’ is a character, so to display it, the ‘%s’ format specifier should be used.
- If we use some other format specifier, you will not get the desired output. In the following case, we are using %d to print a character. Hence it will give ASCII numbers as output.
Example:
2. %d ⇒ denotes the integer variable.
- ‘%d’ format specifier is used to print integers.
Example:
- ‘%d’ can also be used to print decimal values, but it will display in terms of exponential form.
Example:
3. %f ⇒ denotes the floating point variable.
- The ‘%f’ format specifier prints numbers with decimal points. By default, it displays six decimal places.
Example:
- We can vary the decimal places per our wish by defining the number of decimal places in the format specifier.
Conclusion:
This article helps to understand the common data types in Matlab and gives a detailed explanation of the format specifiers with examples.