How to export a structure to a text file?

12 views (last 30 days)
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
Lucia
Lucia on 16 Aug 2013
DATE 15.08.2013
USER Lucia
1 2 3 4 5 6
Lucia
Lucia on 19 Aug 2013
What I would like to do is like "reverse" the "exportdata" function, to get a text file with the headers stored in "textdata" of the structure and the numerical values stored in "data" of the structure. Thanks.

Sign in to comment.

Accepted Answer

dpb
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".

More Answers (0)

Community Treasure Hunt

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

Start Hunting!