Matching certain characters of strings
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (4 件)
Thorsten
2013 年 2 月 25 日
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
2013 年 2 月 25 日
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 件のコメント
Jos (10584)
2013 年 2 月 25 日
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 件のコメント
Azzi Abdelmalek
2013 年 2 月 25 日
編集済み: Azzi Abdelmalek
2013 年 2 月 25 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!