Selecting a particular part of a string variable

3 ビュー (過去 30 日間)
antonet
antonet 2012 年 7 月 11 日
Dear all,
I have
A={'BARI 500 esd' ...
'DUR NOR 18 df '
}
I want to choose the part of the string that is up to the number that appears in each string
That is, for the above example I want to select
'BARI 500'
'DUR NOR 18'
Is there a way to do that?
thanks

採用された回答

Jan
Jan 2012 年 7 月 12 日
編集済み: Jan 2012 年 7 月 12 日
A = {'BARI 500 esd', ...
'DUR NOR 18 df '};
B = cell(size(A));
for i = 1:numel(A)
a = A{i};
index = find(isstrprop(a, 'digit'), 1);
a = a(1:index);
index = find(~isspace(a), 1, 'last'); % hand coded STRTRIM
B{i} = a(1:index);
end
[EDITED] For your edited question:
for i = 1:numel(A)
a = A{i};
index = find(isstrprop(a, 'digit'), 1, 'last');
B{i} = a(1:index);
end
  1 件のコメント
antonet
antonet 2012 年 7 月 12 日
thank you simon!

サインインしてコメントする。

その他の回答 (2 件)

Matt Kindig
Matt Kindig 2012 年 7 月 12 日
Regular expressions might provide an easier way.
fcn = @(s) regexpi(s, '^(\D+)', 'match', 'lineAnchors'); %regex function
B = cellfun( fcn, A); %run on cell array A
B = strtrim(B); %get rid of trailing spaces
  3 件のコメント
Jan
Jan 2012 年 7 月 12 日
@antonet: But this was what you were asking for. Please explain "I want to select" exactly.
antonet
antonet 2012 年 7 月 12 日
編集済み: antonet 2012 年 7 月 12 日
you are right. I edited my initial question . thank you and sorry

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 12 日
編集済み: Azzi Abdelmalek 2012 年 7 月 12 日
A={'BARI 500 esd' 'DUR NOR 18 df '};
a=char(A);
[n,m]=size(a);w=[];
for k=1:n
v=[];testp=1;test=0;test2=0
for h=1:m
if length(regexp(a(k,h),'[0-9]'))>0
test=1,testp=0,
end
if test==0 & testp==1
v=[v a(k,h)],test2=1
elseif test==1 & test2==1
v=[v a(k,h)],test=0
else
test2=0,testp=0,test=0
end
end
w=strvcat(w,v);
end
disp(w)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by