MATLAB: How to export a structure to a text file

exporting a structure to a txt 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!

Best Answer

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