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

采纳的回答

Iain
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.

更多回答(1 个)

David Sanchez
David Sanchez 2013-5-29
you should follow the link above:
It provides a m-file to write cells to txt

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by