Effacer les filtres
Effacer les filtres

Modifying a string

27 vues (au cours des 30 derniers jours)
joseph Frank
joseph Frank le 15 Août 2011
Hi,
I have a cell array where in each cell I have an alpha numeric number of IDs. I want to adjust this array in a way that if the ID is less than 6 digits (assume 777T) i want to add zeros before it to make it a 6 character ID (00777T). I have no clue how to do it.Any help is appreciated.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 15 Août 2011
Use strjust().
a={'777T','123456','23A'};
b=char(a(:));
c=strjust(b,'right');
d=cellstr(c);
e=strrep(d,' ','0');
or make it shorter:
a={'777T','123456','23A'};
c=strjust(char(a(:)),'right');
e=strrep(cellstr(c), ' ','0');

Plus de réponses (2)

Paulo Silva
Paulo Silva le 15 Août 2011
c={{'777T'},{'111T'},{'222T'}}
for n=1:numel(c)
c{n}=['00' char(c{n})];
end
c
or this alternative:
c={{'777T'},{'111T'},{'222T'}}
c=cellfun(@(x) ['00' char(x)],c,'uni',false)
With predefined number of characters
c={{'777T'},{'111T'},{'222T'}}
c=cellfun(@(x) [repmat('0',1,abs(6-numel(char(x)))) char(x)],...
c,'uni',false)

joseph Frank
joseph Frank le 15 Août 2011
that sounds good but how can i figure out how many zeros to add. c in your example consists of 4 characters but in my case it could be 3, 4 , 5 or 6 for example: 0 votes Paulo Silva answered 14 minutes ago
c={{'7177T'},{'111T'},{'22233T'}}

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by