All ProgramsCAEMatLabMatLab Programming

Use Of ‘fprintf()’ In MatLab Programming With Examples

When you are showing your results inside a sentence to program user, you can use ‘fprintf()’ command in Matlab. ‘fprintf()’ is a very extensive command that you can show various variables that are created inside Matlab coding, or taken as inputs from users. In here, we explain how to use ‘fprintf()’ command in Matlab with very basic examples below.

How To Use ‘fprintf()’ Command In MatLab?

Just take a loo ak the very basic example below about the use of ‘fprintf()’ command in Matlab.

 age = 35;
height = 6.1;
fprintf('My age is %f and height is %f.', age, height);
My age is 35.000000 and height is 6.100000.

As you see at example above, we created two of variables in Matlab named ‘age’ and ‘height’. These two variables have values of ’35’ and ‘6.1’ respectively.

We used ‘fprintf()’ command as you see above to show a sentence that includes these two informations to user.

Inside the pharantheses of fprintf() command above, first you can write the sentence inside quotes, which must includes the required conversion indicators like ‘%f’. These conversion indicators will take the value of variables that typed after the sentence inside quotes, just like above ‘age’ and ‘height’ respectively inside pharantheses.

When you are writing the variables inside fprintf() command, you do not need to use quotes. Just put commas between variables.

Conversion indicators will take these values of variables starting from right to left, inside pharanteses. So, a meaningful sentence is created at command window as you see above.

The advantage of ‘fprintf()’ command in Matlab, when the values of variables are changed by the code users or by you, then sentence itself also changes.

Avaliable Conversion Characters As %f For fprintf() Command In Matlab

  • %f: This takes the all decimal values inside sentences. You can define the precision of decimal values by adding precision numbers which is shown below.
  • %e or %E: Exponantial notation which is also called as scientific notation that you can use(For example: 3.3526e+000).
  • %g: If you use this conversion character, the trailing zeros for decimal numbers will not be shown.
  • %u, %d and %i: This conversion character shows the value of variable number in base 10.
  • %o: This conversion character shows the value of variable number in base 8 which is also first letter of octal.
  • %x: This conversion character shows the value of variable number in base 16 in which the numbers between 10-16 are shown letters between a – f.
  • %c: Shows single characters which is the value of a variable in Matlab. This can be useful when you take string informations from users.
  • %s: This is an actually string array which is different from %c.

Adjusting Precision To Displaying Values With’fprintf()’ Command In MatLab

To understand adding precision on your variables in ‘fprintf()’ command, take a look at the example below.

>> way = 3556.55;
velocity = 201.36;
fprintf('We need to take %8.3f km of way with %8.4f km/h velocity', way, velocity);
We need to take 3556.550 km of way with 201.3600 km/h velocity>>

This is a very basic example like first example. We are using two of variables to obtain our sentence at Command Window.

There is a slight difference between this example that includes %8.3f and %8.4f clauses. The decimals 8.3 and 8.4 here specify the precision of values of variables.

YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!

The first number which is ‘8’ these two variables gives the total number at the presentation of variable in sentence at command window.

If you take a look at the sentence in command window which is ‘We need to take 3556.550 km of way with 201.3600 km/h velocity.’, these two variables have 7 numbers to represent to variables defined in ‘fprintf()’ command above. Also there is decimal point between decimals and integers. At total, there is 8 of numbers and decimal point for these two variables at command window. This ‘8’ at %8.3 and %8.4 specifies this value.

Download And Install MatLab 2020!

The second number at %8.3 and %8.4 which are ‘3’ and ‘4’ respectively, defines the number of numbers after decimal point. Take a look at the sentence again you will see three numbers for first variable, and four numbers for second variable, after decimal point.

Use Of After Text Operators In ‘fprintf()’ Command In MatLab

The most common after text operator is ‘\n’ in Matlab. In command window, ‘\n’ starts the code in new line. If you take a look at the above examples, you can see that you can enter new codes right after the sentences.

To take it to the next line, you can use ‘\n’ inside the ‘fprintf()’ code.

>> way = 3556.55;
velocity = 201.36;
fprintf('\nWe need to take %8.3f km of way\n with %8.4f km/h velocity\n', way, velocity);

We need to take 3556.550 km of way
 with 201.3600 km/h velocity

As you see above example, we added some ‘\n’ inside the sentence to arrange the text appearence at command window in Matlab. You can understand the work of ‘\n’ by comparing this example with right before example.

These are the other after text operators that you can use;

  • \r: You can use this instead of \n to make carriage return in Matlab ‘fprintf()’ command.
  • \f: Form feeding in ‘fprintf()’ command in Matlab.
  • \b: Backspacing in ‘fprintf()’ command in Matlab.
  • %%: You can use it to obtain percent character ‘fprintf()’ command in Matlab.
  • \\: This creates a backslash character ‘fprintf()’ command in Matlab.
  • ”: To obtain single quotation mark in your sentences, use this ‘fprintf()’ command in Matlab.

Conclusion For ‘fprintf()’ Command In MatLab

You can understand the general use of ‘fprintf()’ command in MatLab to represent your results of your codes to users. This is very simple and efficient way to do it in Matlab.

If you want further examples about ‘fprintf()’ command in Matlab, please leave your intentions to comments to inform us.

Do not forget to leave your comments and questions about the use of ‘fprintf()’ command in Matlab below.

Your precious feedbacks are very important for us.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button