Is there a function to do this?

Antworten (3)

Stephen23
Stephen23 am 8 Jan. 2018
Bearbeitet: Stephen23 am 8 Jan. 2018

2 Stimmen

There is no need to manipulate the format string, it is much simpler to use the '*' option:
>> sprintf('%0*d',4,12)
ans = 0012
Fangjun Jiang
Fangjun Jiang am 8 Jan. 2018

0 Stimmen

With this, will you be able to work out the function?
>> sprintf('%04d',12)
ans =
'0012'
Guillaume
Guillaume am 8 Jan. 2018

0 Stimmen

sprintf? Describing what you want in words would help
number = 12;
chars = 4;
formatstring = sprintf('%%0%dd', chars); %create format string %0{char}d' where {char} is the value of char
sprintf(formatstring, number)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 8 Jan. 2018

Bearbeitet:

am 8 Jan. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by