How do I add leading zero to integer string in MATLAB 7.8 (R2009a)?
18 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 27 Giu 2009
Commentato: Walter Roberson
il 16 Mar 2017
I want to form a string that has some text, and a number formed by using an integer.
For example, if the integer is 5, I want to form a string -
'SomeText05'. If the integer is 15, I want to form 'SomeText15'.
Strcat('SomeText', int2str(15)) gives me SomeText15, but
strcat('SomeText', int2str(5)) gives me SomeText5. I want SomeText05.
I do not know what format string I should use to get the leading zero before 5.
Risposta accettata
MathWorks Support Team
il 27 Giu 2009
You can use SPRINTF function with the following 'flag' and 'fieldwidth':
sprintf('%02d',5)
To append this formatted string to a fixed string, you can do something as:
strcat('SomeText', sprintf('%02d',5))
strcat('SomeText', sprintf('%02d',15))
To get more information on SPRINTF and different options that you have along with it, please refer to the following documentation page:
<http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sprintf.html>
1 Commento
Walter Roberson
il 16 Mar 2017
With a fixed string, you would probably use something like
sprintf('SomeText%02d', 5)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!