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?
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Dyuman Joshi
il 22 Mar 2024
"Is there any way to achve this?"
Yes, explicitly change the format as and wherever required.
Stephen23
il 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.
Risposte (2)
Manikanta Aditya
il 22 Mar 2024
% Long format for pi
format long
pi_long = pi
disp(['Long format: ', num2str(pi_long)])
% Short format for other values
format short
val_short = 1.1
disp(['Short format: ', num2str(val_short)])
Thanks
2 Commenti
Dyuman Joshi
il 22 Mar 2024
Why are you using disp(), which clealry does not show the output in the specified formats?
Manikanta Aditya
il 22 Mar 2024
I thought when combined with num2str() might do the job but looks like as it converts the number into a string using the current format which might not reflect the long or short format I've set. Thanks for bringing it up.
Image Analyst
il 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 Commenti
Vedere anche
Categorie
Scopri di più su Time Series Objects in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!