Saving cell array with strings to a text file.

조회 수: 5 (최근 30일)
Job
Job 2012년 11월 4일
I'm trying to figure out how to save a chart to a text file. The 'chart' is a cell array that consists of strings. So I have:
a = {'zdfsad' 'xdads' 'zdfsad' 'xdads' 'zdfsad' 'xdads';...
'shs' 'asdbjv' 'affsa' 'asakj' 'asdbjv' 'affsa';...
'zdfsad' 'xdads' 'zdfsad' 'asdbjv' 'affsa' 'zdfsad'}
fid = fopen('output','w');
fprintf(fid, 'title\n\n');
fprintf(fid, a{:});
fclose(fid);
But when I run it and use the 'type output' command it merely returns:
title
zdfsad
I'm pretty sure the issue is with my fprintf command but I can't for the life of me figure out how to use it with a cell array containing strings.
Thanks for your help.

답변 (1개)

Matt Fig
Matt Fig 2012년 11월 4일
편집: Matt Fig 2012년 11월 4일
a = {'zdfsad' 'xdads' 'zdfsad' 'xdads' 'zdfsad' 'xdads';...
'shs' 'asdbjv' 'affsa' 'asakj' 'asdbjv' 'affsa';...
'zdfsad' 'xdads' 'zdfsad' 'asdbjv' 'affsa' 'zdfsad'}
fid = fopen('output.txt','w');
fprintf(fid, 'title\n\n');
str = [repmat('%10s',1,size(a,2)),'\n'];
for ii = 1:size(a,1)
fprintf(fid,str,a{ii,:});
end
fclose(fid);
Now open the file with wordpad...
  댓글 수: 1
Jan
Jan 2012년 11월 4일
Or instead of the loop:
fprintf(fid, str, transpose(a));

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by