Matching certain characters of strings
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Say I have a big cell array of strings (they are item numbers), and I want to say:
If string matches '140xx1', then Type = 1,
If string matches '140xx2' then Type = 2,
If string matches '140xx3' then Type = 3,
If string matches '145xxx' then Type = 4,
(where x can be any character).
How can I do this in MATLAB?
Many thanks,
Chris
0 Kommentare
Antworten (4)
Thorsten
am 25 Feb. 2013
item_number_list = { '140xx3' '140xx3' '140xx1' '145xxx' '140xx2' '140xx3'};
item_numbers = {'140xx1' '140xx2' '140xx3' '145xxx'};
for i=1:length(item_number_list)
item_type(i) = find(strcmp(item_numbers, item_number_list{i}));
end
Thorsten
am 25 Feb. 2013
Oh, I haven't realized that x were meant as placeholders for any number. In this case, use
item_numbers = {'140xx1' '140xx2' '140xx3' '145xxx'};
item_number_list = { '140003' '140013' '140001' '145012' '140022' '140123'};
for i=1:length(item_number_list)
item = item_number_list{i};
item(4:5) = 'xx';
if item(3) == '5', item(6) = 'x'; end
item_type(i) = find(strcmp(item_numbers, item));
end
0 Kommentare
Jos (10584)
am 25 Feb. 2013
I seems that only the last character of the string is of importance:
LIST = {'140111','140aa4','999cc3','123ZZ1','145xxx'}
TYPE = cellfun(@(x) x(numel(x))-'0', LIST)
TYPE(~ismember(TYPE,[1 2 3])) = 4
0 Kommentare
Azzi Abdelmalek
am 25 Feb. 2013
Bearbeitet: Azzi Abdelmalek
am 25 Feb. 2013
yourlist={'140221','140214','140111','140223','140544','140773'}
type1=yourlist(cell2mat(cellfun(@(x) ~isempty(regexp(x,'\<140\d\d1\>')),yourlist,'un',0)))
type2=yourlist(cell2mat(cellfun(@(x) ~isempty(regexp(x,'\<140\d\d2\>')),yourlist,'un',0)))
type3=yourlist(cell2mat(cellfun(@(x) ~isempty(regexp(x,'\<140\d\d3\>')),yourlist,'un',0)))
type4=yourlist(cell2mat(cellfun(@(x) ~isempty(regexp(x,'\<140\d\d4\>')),yourlist,'un',0)))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!