フィルターのクリア

How can I write any cell data into txt file as they appear.

1 回表示 (過去 30 日間)
sermet
sermet 2013 年 5 月 29 日
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 Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by