필터 지우기
필터 지우기

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