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 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 16 Août 2013
Can you give a short example of what you want to write in a text file?
Lucia
Lucia le 16 Août 2013
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.
Lucia
Lucia le 16 Août 2013
DATE 15.08.2013
USER Lucia
1 2 3 4 5 6
Lucia
Lucia le 19 Août 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.

Connectez-vous pour commenter.

 Réponse acceptée

dpb
dpb le 19 Août 2013
Modifié(e) : dpb le 19 Août 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".

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by