MATLAB: How to add leading zeros in certain elements of an array

arrayleading zeros

If I have an array like: 2 25 3 24 and I want to convert it to: 02 25 03 24 How can I do this conversion?

Best Answer

Simple solution without converting to numeric or using regexp:
>> C = strsplit('2 25 3 24');
>> sprintf('%02s ',C{:})
ans =
02 25 03 24
or for older versions:
>> C = regexp('2 25 3 24','\s','split');
>> sprintf('%02s ',C{:})
ans =
02 25 03 24