How to write a value with zeros before the value in matlab?
2 次查看(过去 30 天)
显示 更早的评论
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,
0 个评论
采纳的回答
Adam
2016-11-14
编辑:Adam
2016-11-14
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 个评论
Adam
2016-11-14
编辑:Adam
2016-11-14
In response to what you posted below, the syntax you posted doesn't seem to work at all, to even give what you show, but this works for me:
file=sprintf('%s%s_%09dO.nc', directory,'test',mynumber)
It is the rest of your sprintf format specification and input arguments that appear to not be quite right. For a start you specify 4 distinct fields, but only supply 3 inputs. The final %s that you have should not be there and mynumber should not be in a string.
更多回答(1 个)
Farshid Daryabor
2016-11-14
1 个评论
Adam
2016-11-14
Please add as a comment to an answer rather than a new answer as it is less confusing.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!