How to export a structure to a text file?

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

Can you give a short example of what you want to write in a text file?
DATE 15.08.2013 USER Lucia
1 2 3 4 5 6 I get in "textdata" the first two rows with the header, and in "data" the matrix with the numbers. I would like to change the values in the matrix (in "data") and export again to a text file with same format as the former example.
DATE 15.08.2013
USER Lucia
1 2 3 4 5 6
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)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 16 Aug 2013

Community Treasure Hunt

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

Start Hunting!