how to format integer numbers to start with filling zeros?

101 views (last 30 days)
I would like to transform n = 1:12 to '01','02',...,'11','12'

Accepted Answer

Stephen23
Stephen23 on 15 Dec 2015
Edited: Stephen23 on 15 Dec 2015
This only makes context in terms of strings, in which case sprintf makes this easy:
>> sprintf('%02d ',1:12)
ans = 01 02 03 04 05 06 07 08 09 10 11 12
And if you want them in a cell array just use strsplit on it, or cellstr.

More Answers (1)

Walter Roberson
Walter Roberson on 15 Dec 2015
cellstr(num2str((1:12).', '%02d'))
or
arrayfun(@(x) sprintf('%02d', x), 1:12, 'Uniform', 0);

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!