Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Find Jobs


For Business / Universities

Corporate Training

Hire from US

Academic Up-skilling


All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Mechanical

Uploaded on

06 Dec 2022

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

logo

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

author

Navin Baskar


Author

blogdetails

Skill-Lync

img

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.

Mechanical

10 May 2020


What is Finite element analysis (FEA)?

What exactly is Finite element analysis? How would one explain the basic concept to an undergrad friend? Learn how FEA courses at Skill-Lync can help you get employed.

Mechanical

08 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.

Mechanical

10 May 2020


What is Strain Hardening and Plasticity?

This effect shown in the kinematic hardening is known as the Bauschinger effect. For large strain problems, kinematic hardening model is not a good choice because of the Bauschinger effect.

Mechanical

27 Jun 2022


What is Hourglass Effect? Working & Fixes

When the elements in a model deform but the strain energy is not computed for that deformation, it results in an Hourglass deformation effect or Hourglass effect. Usually, this can be clearly seen in the post-processing stage where the elements will have a zig-zag formation.

Mechanical

28 Jun 2022



Author

blogdetails

Skill-Lync

img

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.

Mechanical

10 May 2020


What is Finite element analysis (FEA)?

What exactly is Finite element analysis? How would one explain the basic concept to an undergrad friend? Learn how FEA courses at Skill-Lync can help you get employed.

Mechanical

08 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.

Mechanical

10 May 2020


What is Strain Hardening and Plasticity?

This effect shown in the kinematic hardening is known as the Bauschinger effect. For large strain problems, kinematic hardening model is not a good choice because of the Bauschinger effect.

Mechanical

27 Jun 2022


What is Hourglass Effect? Working & Fixes

When the elements in a model deform but the strain energy is not computed for that deformation, it results in an Hourglass deformation effect or Hourglass effect. Usually, this can be clearly seen in the post-processing stage where the elements will have a zig-zag formation.

Mechanical

28 Jun 2022


Book a Free Demo, now!

Related Courses

https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/introduction-hev-matlab-simulink_1612262875.jpg
Introduction to Hybrid Electric Vehicle using MATLAB and Simulink
4.8
23 Hours of content
Electrical Domain
Know more
https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/vehicle-dynamics-matlab_1636606203.png
4.8
37 Hours of content
Cae Domain
Showing 1 of 4 courses
Try our top engineering courses, projects & workshops today!Book a Live Demo