Splitting the elements in the cell array

조회 수: 58 (최근 30일)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2016년 10월 28일
편집: Gopalakrishnan venkatesan 2018년 12월 18일
I have a cell array,
a = { abcdsfa_def , ef_ghi, higdsfasfa_klm}
Now i need to remove the each element in the cell array from '_'.
my answer should be a = {abcdsfa, ef, higdsfasfa}
Thanks a lot
  댓글 수: 1
Jan
Jan 2016년 10월 28일
편집: Jan 2016년 10월 28일
Why does splitting 'abcdsfa_def' at '_' yield 'abc'? I'd expect 'abcdsfa'. 'efg' looks even more strange.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2016년 10월 28일
While this does not match the shown output, it matches your explanations:
a = {'abcdsfa_def', 'ef_ghi', 'higdsfasfa_klm'}
r = strtok(a, '_')

추가 답변 (1개)

KSSV
KSSV 2016년 10월 28일
a = { 'abc_def' , 'efg_ghi', 'hig_klm'}
b = cellfun(@(x) x(1:3), a, 'UniformOutput', false)
  댓글 수: 2
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2016년 10월 28일
Number of elements before underscore is not all alway three, a = {abcdef_dasf, as_dfafdsa} What to do in this case ?
KSSV
KSSV 2016년 10월 28일
a = {'abcdef_dasf', 'as_dfafdsa'} ;
b = cell(size(a)) ;
for i= 1:length(a)
t = strsplit(a{i},'_') ;
b{i} = t{1} ;
end
b

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by