Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Student Reviews

More

Academic Training

Informative Articles

Find Jobs

We are Hiring!

All Courses

Choose a category

Loading...

All Courses

All Courses

Modified on

06 Dec 2022 06:56 pm

# Importance of Format Specifier(%s,%d,%f)

Skill-Lync

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.

Author

Navin Baskar

Author

Skill-Lync

Subscribe to Our Free Newsletter

Continue Reading

Related Blogs

Shock tube simulation

Learn how to render a shock-tube-simulation and how to work on similar projects after enrolling into anyone of Skill-Lync's CAE courses.

10 May 2020

Design of Frontal BIW enclosure of a car (Bonnet)

In this blog, read how to design the frontal BIW enclosure of a car (Bonnet) and learn how Skill-Lync Master's Program in Automotive Design using CATIA V5 will help you get employed as a design engineer.

10 May 2020

What is Tetra Meshing?

Tetrahedral is a four- nodded solid element that can be generated through the tria element by creating a volume and also through the existing volume of the geometry. These elements are used where the geometry has high thickness and complexity. The image attached below is a representation of a Tetra element. The Tetra element will have 4 triangular faces with four nodes joining them together

02 Aug 2022

Realizing Connectors In HyperMesh

A connector is a mechanism that specifies how an object (vertex, edge, or face) is connected to another object or the ground. By often simulating the desired behaviour without having to build the precise shape or specify contact circumstances, connectors make modeling simpler.

03 Aug 2022

Mesh Sizing In Ansys Workbench

One of the most crucial processes in carrying out an accurate simulation using FEA is meshing. A mesh is composed of elements that have nodes—coordinate positions in space that might change depending on the element type—that symbolise the geometry's shape.

04 Aug 2022

Author

Skill-Lync

Subscribe to Our Free Newsletter

Continue Reading

Related Blogs

Shock tube simulation

Learn how to render a shock-tube-simulation and how to work on similar projects after enrolling into anyone of Skill-Lync's CAE courses.

10 May 2020

Design of Frontal BIW enclosure of a car (Bonnet)

In this blog, read how to design the frontal BIW enclosure of a car (Bonnet) and learn how Skill-Lync Master's Program in Automotive Design using CATIA V5 will help you get employed as a design engineer.

10 May 2020

What is Tetra Meshing?

Tetrahedral is a four- nodded solid element that can be generated through the tria element by creating a volume and also through the existing volume of the geometry. These elements are used where the geometry has high thickness and complexity. The image attached below is a representation of a Tetra element. The Tetra element will have 4 triangular faces with four nodes joining them together

02 Aug 2022

Realizing Connectors In HyperMesh

A connector is a mechanism that specifies how an object (vertex, edge, or face) is connected to another object or the ground. By often simulating the desired behaviour without having to build the precise shape or specify contact circumstances, connectors make modeling simpler.

03 Aug 2022

Mesh Sizing In Ansys Workbench

One of the most crucial processes in carrying out an accurate simulation using FEA is meshing. A mesh is composed of elements that have nodes—coordinate positions in space that might change depending on the element type—that symbolise the geometry's shape.

04 Aug 2022

Book a Free Demo, now!

Related Courses

Introduction to Hybrid Electric Vehicle using MATLAB and Simulink
4.8
23 Hours of content
Electrical Domain
4.8
37 Hours of content
Cae Domain
Showing 1 of 4 courses