How can I write any cell data into txt file as they appear.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Iain
2013-5-29
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 个评论
更多回答(1 个)
David Sanchez
2013-5-29
you should follow the link above:
It provides a m-file to write cells to txt
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!