Saving cell array with strings to a text file.
Show older comments
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.
Answers (1)
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 Comment
Jan
on 4 Nov 2012
Or instead of the loop:
fprintf(fid, str, transpose(a));
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!