When you are showing your results inside a sentence to program user, you can use the ‘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.

If you are interested to learn Matlab® at an engineering level, click on the given link or the ‘Shop Now’ button to check the recommended book by Mechanicalland, from Amazon!

## How To Use ‘fprintf()’ Command In Matlab®?

Just take a look at the very basic example below about the use of the ‘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 in the example above, we created two variables in Matlab® named ‘age’ and ‘height’. These two variables have values of ’35’ and ‘6.1’ respectively.

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

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

When you are writing the variables inside the 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 parentheses. So, a meaningful sentence is created at the command window as you see above.

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

## Available Conversion Characters As %f For fprintf() Command In Matlab®

**%f:**This takes all decimal values inside sentences. You can define the precision of decimal values by adding precision numbers which are shown below.**%e**or %E: Exponential notation which is also called 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 the variable number in base 10.**%o:**This conversion character shows the value of the variable number in base 8 which is also the first letter of octal.**%x:**This conversion character shows the value of the 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 information from users.**%s:**This is an actual string array that is different from %c.

## Adjusting Precision To Displaying Values With ‘fprintf()’ Command In Matlab®

To understand adding precision on your variables in the ‘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>>
```

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

This is a very basic example like the first example. We are using two 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 the variable in a sentence at the command window.

If you take a look at the sentence in the command window which is ‘We need to take 3556.550 km of the 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 the decimal point between decimals and integers. In total, there is 8 numbers and decimal point for these two variables at the command window. This ‘8’ at %8.3 and %8.4 specifies this value.

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

## Use Of After Text Operators In ‘fprintf()’ Command In Matlab®

The most common after text operator is ‘\n’ in Matlab®. In the command window, ‘\n’ starts the code in a 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 in the above example, we added some ‘\n’ inside the sentence to arrange the text that appears at the command window in Matlab®. You can understand the work of ‘\n’ by comparing this example with the 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 the Matlab® ‘fprintf()’ command.**\f:**Form feeding in ‘fprintf()’ command in Matlab®.**\b:**Backspacing in ‘fprintf()’ command in Matlab®.**%%:**You can use it to obtain the percent character ‘fprintf()’ command in Matlab®.**\\:**This creates a backslash character ‘fprintf()’ command in Matlab®.**”:**To obtain a 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 the ‘fprintf()’ command in Matlab® to represent the results of your codes to users. This is a very simple and efficient way to do it in Matlab®.

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

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

Your precious feedbacks are very important for us.