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