Extracting text from a cell

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일

0 개 추천

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

joseph Frank
joseph Frank 2013년 7월 24일
No it is not working because I have many observations and not simply one. For example: A={'00003H102';'000165100'}; how can I get B={'00003H';'000165'};
joseph Frank
joseph Frank 2013년 7월 24일
I am receiving an error Index exceeds matrix dimensions.
Error in @(x)x(1:6) the array is 30000x1 . it is possible I think that I have rows will less than 6 characters. IS there a way to overcome this problem by selecting the first 6 letters and numbers only if the number of characters is more or equal to 6?
Narges M
Narges M 2013년 7월 24일
ofcourse there is. have 6 be a variable: var = 6. then use an if-statement to change the value in var, in case the length of the current row is less than 6.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 24일
Yes it's possible
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일

0 개 추천

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일

0 개 추천

B = regexp(A,'^.{0,6}','match')
B = cat(1,B{:});

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2013년 7월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by