Is it possible to use format long in one line of code and then use format short in another line of code on the same script?
7 views (last 30 days)
Show older comments
Bascially I am required to write pi in long format in the beginning of my script but later on I need to write values like 1.1 or 1.3, and I would like to do this part in short format. Is there any way to achve this?
2 Comments
Dyuman Joshi
on 22 Mar 2024
"Is there any way to achve this?"
Yes, explicitly change the format as and wherever required.
Stephen23
on 22 Mar 2024
"Is there any way to achve this? "
Sure, as other have shown you can fiddle around with the FORMAT. But a cleaner and more generalized approach would be to forget about the command windows settings: just use FPRINTF and specify the format explicitly.
Answers (1)
Image Analyst
on 22 Mar 2024
Just call format whenever you want to set the format and it will be that format for the rest of the script (or until you call format again)
format short g
pi
format long g
pi
format short g
1.1
1.3
format long g
1.1
1.3
% Use sprintf to display with specified field width, for example 5, 3, or 23:
fprintf('%.5f', pi)
fprintf('%.3f', 1.1)
fprintf('%.23f', 1.3)
Note: format does not have any effect on fprintf. The number of decimal places shown when you use fprintf is the number after the percent symbol and dot.
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!