how to use format spec in fprintf properly
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Itzik Ben Shabat
am 16 Nov. 2013
Kommentiert: Itzik Ben Shabat
am 16 Nov. 2013
Hello,
I have read the help documentation but somtehing doesnt work as expected. say i have a variable
x=3.14
and i want to print to the command window the value of x while allowing 3 digits bedfore the point and 3 after. so i expect to get 003.140 i used
fprintf('%03.3f\n',x)
and got
3.140 as output.
what went wrong ? thanks
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 16 Nov. 2013
The first number is the total field width, INCLUDING the number after the decimal point. It is not the number before, dot, and then the number after. So if you wanted 3 before, a decimal point, and then 3 more, you'd have to do %7.3f.
2 Kommentare
Image Analyst
am 16 Nov. 2013
Examples:
>> fprintf('%07.3f\n', pi)
003.142
>> fprintf('%7.3f\n', pi)
3.142
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!