필터 지우기
필터 지우기

concatenate cell

조회 수: 2 (최근 30일)
Alexandros
Alexandros 2011년 12월 20일
답변: Eric Tao 2018년 2월 3일
i have a cell (symbol {} on the variable list) with a=('kkk', 'lll', 'xxx', 'jjj.xls')
and i want to concatenate a so that i can have
b = ('kkk_lll_xxx_jjj.xls')
thank you
  댓글 수: 2
Alexandros
Alexandros 2011년 12월 20일
They are not with , (commas separated) they are separated with ; so they are one under the other. but i want one string at the end
Alexandros
Alexandros 2011년 12월 20일
I don't want to separate a because it can be of infinite dimensions. i need to concatenate as much as information i have in the initial cell

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 12월 20일
a={'kkk', 'lll', 'xxx', 'jjj.xls'}';
out = [a(:)';repmat({'_'},1,numel(a))];
out = [out{:}];
out = out(1:end-1);
variant 2
out = cell(1,2*numel(a)-1);
out(1:2:end) = a;
out(2:2:end) = {'_'};
out = [out{:}];
  댓글 수: 1
Alexandros
Alexandros 2011년 12월 20일
This is perfect but it puts me a '_' also at the end which i dont need
maybe i could delete it with taking only out(1:end-1)?

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

추가 답변 (4개)

Jan
Jan 2011년 12월 20일
a = {'kkk'; 'lll'; 'xxx'; 'jjj.xls'}
out = sprintf('%s_', a{:});
out(end) = [];
or:
out = [sprintf('%s_', a{1:end-1}), a{end}];
or:
out = sprintf([repmat('%s_', 1, numel(a)-1), '%s'], a{:});
For very large cell strings, this becomes slow because Matlab forgets to pre-allocate the output properly. Therefore I've written the C-mex function CStr2String:
out = CStr2String(a, '_', 'noTrial')
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2011년 12월 20일
Hi Jan! +1.

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


Alexandros
Alexandros 2011년 12월 20일
This is perfect but it puts me a '_' also at the end which i dont need
maybe i could delete it with taking only out(1:end-1)?

Alexandros
Alexandros 2011년 12월 20일
This is very helpul guys
Thank you

Eric Tao
Eric Tao 2018년 2월 3일
just type:
b = {strjoin(a,'_')};
Then your b will be a cell as
{'kkk_lll_xxx_jjj.xls'}

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by