Help me print this cell array...

조회 수: 9 (최근 30일)
David Pesetsky
David Pesetsky 2018년 6월 27일
댓글: Star Strider 2018년 6월 27일
I've attached a cell array to this post. Can ANYONE help me just print it to a comma delimet file? Or text file is fine. Will always have 3 columns. Could have a few more rows.
Thank you!

채택된 답변

Star Strider
Star Strider 2018년 6월 27일
The problem is the first column, a cell containing a cell containing a string. The loop is necessary because that is the only way to use fprintf to produce lines of different variable types.
Try this:
D = load('report.mat');
C = D.report;
fid = 1; % This Prints To The Command Window, Use ‘fopen’ In Your Code
for k1 = 1:size(C,1)
str = C{k1,1}{:};
fprintf('%s,%f,%f\n', str,C{k1,2},C{k1,3})
end
fprintf(fid,'\n')
fclose(fid);
producing:
GI,0.361775,25128.000000
GK,0.368588,41753.000000
HG,0.468290,25128.000000
Change the numeric format descriptors as necessary. You can of course eliminate the separate ‘str’ variable and use the fprintf call as:
fprintf('%s,%f,%f\n', C{k1,1}{:}, C{k1,2}, C{k1,3})
You will have to use textscan to read it.
  댓글 수: 2
David Pesetsky
David Pesetsky 2018년 6월 27일
Thank you. This worked.
Star Strider
Star Strider 2018년 6월 27일
As always, my pleasure.

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

추가 답변 (0개)

카테고리

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