How can I write any cell data into txt file as they appear.
3 views (last 30 days)
Show older comments
for example;
data= { 'a' 1 2 3 ; 'b' 4 5 6 }
startingFolder = 'C:\Program Files\MATLAB'
if ~exist(startingFolder, 'dir')
startingFolder = pwd
end
defaultFileName = fullfile(startingFolder, '*.txt')
[baseFileName, folder] = uiputfile(defaultFileName, 'Select a file')
if baseFileName == 0
return
end
fullFileName = fullfile(folder, baseFileName)
fid = fopen(fullFileName, 'wt')
fwrite(fid, data) %error using fwrite Cannot write value: unsupported class cell
fclose(fid)
I wanna write numbers as ASCII format with characters. like;
a 1 2 3
b 4 5 6
0 Comments
Accepted Answer
Iain
on 29 May 2013
for j = 1:size(data,1)
for i = 1:size(data,2)
if ischar(data{j,i})
fwrite(fid,[data{j,i} ' '],'char');
else
fwrite(fid,[num2str(data{j,i}) ' '],'char');
end
end
fwrite(fid,[10 13],'char')
end
This: loops through data, and writes each element and a space after every value (change it to ',' for commas or 9 (iirc) for tab spaces; after each row has been written it writes the new line characters (it might be [13 10] or [10 13] I usually need to double check), and then continues. I haven't double--checked the code so there may be an error ro two.
0 Comments
More Answers (1)
David Sanchez
on 29 May 2013
you should follow the link above:
It provides a m-file to write cells to txt
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!