Hello,
I'm stuck with one problem for a few hours right now. I want to add commas after my cell array string. Right now it looks like:
a = {'a1','a2','a3','a4','a5','a6'}; a(:)'
ans =
'a1' 'a2' 'a3' 'a4' 'a5' 'a6'
But I need it to be like this:
'a1' , 'a2' , 'a3' , 'a4' , 'a5' , 'a6'
(No comma after last one). I tried a lot of ways, but it seems that I can't create my strings in cell array in form of 'a1', initially. strjoin and strcat doesn't seem to solve this problem as well. I'm kinda lost.

 채택된 답변

Star Strider
Star Strider 2015년 11월 25일

0 개 추천

A cell array is by definition a Comma-Separated List. If you want it to appear in your Command Window as one with distinct commas, this works:
a = {'a1','a2','a3','a4','a5','a6'};
fprintf(1, 'a = ')
for k1 = 1:length(a)-1
fprintf(1, '%s , ', char(a(k1)'))
end
fprintf(1, '%s\n', char(a(end)))
a = a1 , a2 , a3 , a4 , a5 , a6

댓글 수: 5

Thanks, but the problem with your answer is that it appears without uppercase quotes:
a = a1 , a2 , a3 , a4 , a5 , a6
While I need them exactly as:
'a1' , 'a2' , 'a3' , 'a4' , 'a5' , 'a6'
Kirby Fears
Kirby Fears 2015년 11월 25일
편집: Kirby Fears 2015년 11월 25일
You can do this without using char().
a = {'a1','a2','a3','a4','a5','a6'};
fprintf(1, 'a = ')
for k1 = 1:length(a)-1
fprintf(1, '%s , ', a{k1})
end
fprintf(1, '%s\n', a{end})
Incorporating Kirby’s Comment (thank you!) this will give you the quotes as well:
a = {'a1','a2','a3','a4','a5','a6'};
fprintf(1, 'a = ')
for k1 = 1:length(a)-1
fprintf(1, '''%s'' , ', a{k1})
end
fprintf(1, '''%s''\n', a{end})
a = 'a1' , 'a2' , 'a3' , 'a4' , 'a5' , 'a6'
Thanks!
Star Strider
Star Strider 2015년 11월 25일
My (and Kirby’s) pleasure!

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

추가 답변 (0개)

카테고리

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

질문:

2015년 11월 25일

댓글:

2015년 11월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by