Extracting text from a cell

13 ビュー (過去 30 日間)
joseph Frank
joseph Frank 2013 年 7 月 24 日
A='00003H102'
How can I write a code to extract the first 6 letters and numbers. i.e. I want B='00003H';
  1 件のコメント
Jan
Jan 2013 年 7 月 24 日
編集済み: Jan 2013 年 7 月 24 日
@joseph: From the comments later on we see, that your data are not A='00003H102', but that you talk of a cell string. Please care for posting the real input data, because posting wrong answers for wrong inputs wastes your and our time. Thanks.
What is the wanted result, when the string has less than 6 characters?

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

回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 24 日
編集済み: Azzi Abdelmalek 2013 年 7 月 24 日
A='00003H102'
A(1:6)
If you have a cell array
A={'00003H102','00003H103','00003H104'}
B=cellfun(@(x) x(1:6),A,'un',0)
  5 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 24 日
Yes it's possible
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 24 日
A={'00003H102','00003H103','00003H104','00003'}
idx=cellfun(@(x) min(numel(x),6),A,'un',0)
B=cellfun(@(x,y) x(1:y),A,idx,'un',0)

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


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 24 日
A={'00003H102','00003H103','00003H104','00003' []}
B=cellfun(@(x) x(1:min(numel(x),6)),A,'un',0)

Andrei Bobrov
Andrei Bobrov 2013 年 7 月 24 日
編集済み: Andrei Bobrov 2013 年 7 月 24 日
B = regexp(A,'^.{0,6}','match')
B = cat(1,B{:});

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by