Next Post:

Subtracting 3D Bodies From Themselves In Siemens NX(Illustrated Expression)

Displaying Expressions In MatLab® With fprintf() Command(Illustrated Expression)

Matlab® provides various kinds of demonstration codes and options to the user to display their variables and expressions in Command Window. This is a very useful thing for the users of the created code. One of these ways to demonstrate the variables or expressions in Matlab® is the fprintf() command. In this article, we will explain the use of the fprintf() command in Matlab®.

How To Use fprintf() Command In MatLab®?

>> t = 25.356;
fprintf('temperature = %2.1f',t);
fprintf('temperature = %2.1e',t);
fprintf('temperature = %2.1g',t);
temperature = 25.4temperature = 2.5e+01temperature = 3e+01>> 

We simply created a variable ‘t’ to represent the temperature value. The use of the fprintf() command is like above that you need to type the expression first as ‘expression = %2.1’, then the variable that you want to display directly after a comma as shown above.

You can define the demonstration of the variable inside the expressions as above. The codes inside the expressions such as = %2.1f means that;

%2.1f; show the variable as two integer numbers and one decimal number after the comma(2.1). ‘f’ expression means that show the number or variable directly as shown above.

%2.1e; show the variable as two integer numbers and one decimal number after the comma(2.1). ‘e’ expression means that show the number or variable as in scientific format as shown above.

%2.1g; show the variable as two integer numbers and one decimal number after the comma(2.1). ‘g’ expression means that show the number or variable as in exponential format as shown above.

t = 25.356;
fprintf('temperature = %2.1f \n',t);
fprintf('temperature = %2.1e \n',t);
fprintf('temperature = %2.1g \n',t);

If you do not use ‘\n’ at the fprintf command in Matlab®, the whole expressions will be displayed as above. We used ‘\n’, and the expressions are displayed regularly in rows as shown above in Matlab®.

Conclusion

So the logic of the fprintf() command to display variables, numbers, or characters in Matlab® is simple like that.

Leave your comments and questions below!

If you want further coding examples about the fprinf command in Matlab®, inform us in the comments.

This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®

Your precious feedbacks are very important to us.

  • Site Comments

At least 10 characters required.