How to write a value with zeros before the value in matlab?

3 views (last 30 days)
Dear,
how to write/print a value with zeros before the value by the sprintf function, e.g., writing/printing the following form;
test_007120128O.nc
data_013587626O.nc
Regards,

Accepted Answer

Adam
Adam on 14 Nov 2016
Edited: Adam on 14 Nov 2016
sprintf( '%05.3d', 20 )
for example will pad leading 0s upto the number you specify (5 in this case, including the 20, so 3 additional 0s), but to get what you want depends what exactly you are starting with. If you always want 9 digits then
sprintf( '%09d', yourNumber )
should work
doc sprintf
covers this if you click on the 'formatSpec' hyperlink.
  2 Comments
Farshid Daryabor
Farshid Daryabor on 14 Nov 2016
Dear Adam,
Thank you very much, it's work perfect.
Best Regards

Sign in to comment.

More Answers (1)

Farshid Daryabor
Farshid Daryabor on 14 Nov 2016
Dear Adam,
Thank you for reply,
I tried the command you suggested me, but unfortunately it doesn't work.
>>directory = 'H:\home works\my folder\';
>>mynumber = 7120128;
>>file=sprintf('%s%s_%09d%sO.nc', directory,'test','mynumber')
>>file = H:\home works\my folder\test_000000055120128O.nc
file should be; file = H:\home works\my folder\test_007120128O.nc
Regards,
  1 Comment
Adam
Adam on 14 Nov 2016
Please add as a comment to an answer rather than a new answer as it is less confusing.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!