To write a text file readable by the importdata function
3 views (last 30 days)
Show older comments
Hi all,
I use the importdata function on an ascii file and extract a structure containing the cell array textdata. Let say that my structure is called 'donnee' :
donnee = importdata(C:\users\Quentin\Mes Documents\Data.txt');
I then work with the information that is inside each cell of donnee.textdata and I modify donnee.textdata.
I would then like to write to a new text file containing my donnee.textdata cell array. I do it in the following way :
fid2 = fopen('C:\users\Quentin\Mes Documents\Data-updated.txt');
size2 = size(donnee.textdata);
n = size2(1); %number of lines
m = size2(2); %number of columns
for i = 1:n
for j=1:m
fprintf(fid2,'%s',donnee.textdata{i,j});
fprintf(fid2,'\t');
end
fprintf(fid2,'\n');
end
fclose(fid2);
Everything works and when I read with Excel for instance, it looks very good ! My problem is when I want to use importdata on the file I created, it doesn't work. The delimiters '\t' are not taken into account and I have a struct only containing one char array per line and not all the lines (80 over 170000).
I tried to use other functions such as dlmread, dlmwrite, struct2File, etc...
The problem is from when I am writig or when I am reading I guess. Anyone has an idea of how I could do ?
Do not hesitate to ask me any precision/code details.
Thank you very much !
Quentin
2 Comments
Walter Roberson
on 7 Sep 2013
More efficient would be:
for i = 1 : n
fprintf(fid2, '%s\t', donnee.textdata{i,1:end-1});
fprintf(fid2, '%s\n', donnee.textdata{i,end});
end
Answers (0)
See Also
Categories
Find more on Text Files 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!