How to horizontally concatenate the cells of a cell array with different contents into a string.

조회 수: 24 (최근 30일)
Hi All,
I have a cell array as the one below:
EXTR =
20×3 cell array
{[ 550]} {[2]} {'GAM'}
{[ 600]} {[2]} {'GAM'}
{[ 650]} {[2]} {'GAM'}
{[ 700]} {[2]} {'GAM'}
{[ 750]} {[2]} {'GAM'}
{[ 800]} {[2]} {'GAM'}
{[ 850]} {[2]} {'GAM'}
{[ 900]} {[2]} {'GAM'}
{[ 950]} {[2]} {'GAM'}
{[1000]} {[2]} {'GAM'}
{[1100]} {[2]} {'GAM'}
{[1200]} {[2]} {'GAM'}
{[1300]} {[2]} {'GAM'}
{[1400]} {[2]} {'GAM'}
{[1500]} {[2]} {'GAM'}
{[1600]} {[2]} {'GAM'}
{[1700]} {[2]} {'GAM'}
{[1800]} {[2]} {'GAM'}
{[1900]} {[2]} {'GAM'}
{[2000]} {[2]} {'GAM'}
How is it possible to create a cell array that looks like this:
EXTR =
20×3 cell array
{'550 2 GAM'}
{'600 2 GAM'}
{'650 2 GAM'}
.....
In words, I would like all my columns to be combined into one where their contents are a string with a single space between them.
I have tried things in the like of horzcat(EXTR{:}), however they are not working for this case, possibly because not all contents are of uniform type...
Thanks for your help in advance,
KMT.

채택된 답변

madhan ravi
madhan ravi 2018년 11월 8일
편집: madhan ravi 2018년 11월 8일
c={{[ 550]} {[2]} {'GAM'}
{[ 600]} {[2]} {'GAM'}}
c=cellfun(@string,c)
join(c)
command window:
>> COMMUNITY
c =
2×1 string array
"550 2 GAM"
"600 2 GAM"
>>
  댓글 수: 3
madhan ravi
madhan ravi 2018년 11월 8일
if you want to make it as cell array just put curly braces in and around i.e.,
c={join(c)} %at the end

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

추가 답변 (1개)

Stephen23
Stephen23 2018년 11월 8일
편집: Stephen23 2018년 11월 8일
An old-fashioned way (strings are probably easier):
>> C = {550,2,'GAM';600,2,'GAM';650,2,'GAM';700,2,'GAM';750,2,'GAM';800,2,'GAM';850,2,'GAM';900,2,'GAM';950,2,'GAM';1000,2,
'GAM';1100,2,'GAM';1200,2,'GAM';1300,2,'GAM';1400,2,'GAM';1500,2,'GAM';1600,2,'GAM';1700,2,'GAM';1800,2,'GAM';1900,2,'GAM';
2000,2,'GAM'};
>> D = C.';
>> S = sprintf('\n%d %d %s',D{:});
>> Z = regexp(S(2:end),'\n','split');
>> Z{:}
ans = 550 2 GAM
ans = 600 2 GAM
ans = 650 2 GAM
ans = 700 2 GAM
ans = 750 2 GAM
ans = 800 2 GAM
ans = 850 2 GAM
ans = 900 2 GAM
ans = 950 2 GAM
ans = 1000 2 GAM
ans = 1100 2 GAM
ans = 1200 2 GAM
ans = 1300 2 GAM
ans = 1400 2 GAM
ans = 1500 2 GAM
ans = 1600 2 GAM
ans = 1700 2 GAM
ans = 1800 2 GAM
ans = 1900 2 GAM
ans = 2000 2 GAM

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by