How to export a structure to a text file?
12 views (last 30 days)
Show older comments
Hello!
I used "importdata" to import the data inside a input.txt file, so I get a Structure which conatains: "data"(with numerical data) and "textdata"(with text data).
I would like to substitute the "data" matrix for another one with corrected values, and then export the complete structure again, to get a file with same format as input.txt with corrected numerical values.
Y tried to do it using dlmwrite, but eventhoug I specify the number of row (C) in which start pasting the numerical data, it erases the lines befoer C.
Any idea?
Thanks!
4 Comments
Accepted Answer
dpb
on 19 Aug 2013
Edited: dpb
on 19 Aug 2013
There is no builtin functionality that will do that transparently, unfortunately. You'll have to build the new file
m = importdata(yourfile);
% mung on m.data here...
fid=fopen('new.txt','w'); % open a new file for writing
for ix=1:length(m.textdata)
fprintf(fid,'%s\n',char(m(i).textdata)); % write the header lines
end
fprintf(fid,'%d ',m.data') % then the data
fid=fclose(fid); % close the new file..
type new.txt % see the new result...
You can overwrite the original file but that's dangerous unless you've thoroughly tested your script/function first, so better to write a new file then rename the old while getting started.
Again, afaik there's no total packaged function that will reproduce the above automagically -- seems like a logical extension but to this point "it ain't happened yet".
0 Comments
More Answers (0)
See Also
Categories
Find more on Low-Level File I/O 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!