How to separate characters in a cell array by commas

조회 수: 1 (최근 30일)
Joel Olenga
Joel Olenga 2022년 7월 5일
댓글: Joel Olenga 2022년 7월 6일
Hello I have the following code:
e = [3 0 -6];
n = length(e);
e_str = cell(1,n);
for i = 1:n
e_str(1,i) = {num2str(e(i))};
end
disp(e_str)
{'3'} {'0'} {'-6'}
I'd like to separete the output with commas as follows:
{'3'} {','} {'0'} {','} {'-6'}

채택된 답변

Chunru
Chunru 2022년 7월 5일
Wondering why you want that. But it can be done as follows:
e = [3 0 -6];
n = length(e);
e_str = cell(1,2*n-1);
for i = 1:n
e_str(1, 2*i-1) = {num2str(e(i))};
if i<n
e_str(1, 2*i) ={','};
end
end
disp(e_str)
{'3'} {','} {'0'} {','} {'-6'}
  댓글 수: 6
Stephen23
Stephen23 2022년 7월 5일
e = [3,-9];
s = join(string(e),',')
s = "3,-9"
Joel Olenga
Joel Olenga 2022년 7월 6일
Even better! thank you Stephen23!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by