Writing an output to a text file
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I was able to develop the code below. But in the command window I can see the output the way I want to produce. But the test.txt file is blank.
Command window output:
3000001 -99.9 -99.9 0.0
3000002 -99.9 -99.9 0.0
3000003 -99.9 -99.9 0.0
3000004 -99.9 -99.9 0.0
3000005 -99.9 -99.9 0.0
3000006 -99.9 -99.9 0.0
3000007 -99.9 -99.9 0.0
3000008 -99.9 -99.9 0.0
3000009 -99.9 -99.9 0.8
3000010 -99.9 -99.9 0.0
I dont understand what I am doing wrong here and why the test.txt file is bank. I want the command window output to be written in the text file.
sample=dlmread('sample.txt',' ','A2..C11');
Data=[sample(1:10,:) Gist1(1:10,5)];
dlmwrite('test.txt',Data,'delimiter',' ','precision', 10)
fid = fopen('test.txt','wt');
for row = 1 : size(Data, 1);
fprintf('%d %.1f %.1f %3.1f\n', Data(row, :));
end
fclose(fid);
Can somebody help me to figure out what I am missing here.
Many thanks in advance.
2 commentaires
Cedric
le 24 Mai 2014
In addition to Geoff's answer, you should either call DLMWRITE (which outputs to file), or implement the loop with the correction brought by Geoff, but not both otherwise you will output the data twice to file.
Image Analyst
le 24 Mai 2014
Modifié(e) : Image Analyst
le 24 Mai 2014
Now you have 3 threads going on the same question. Why? Why not just get this solved in your original thread (which I answered): http://www.mathworks.com/matlabcentral/answers/130865#answer_138030 Why do you want to look in 3 different places?
Réponses (1)
Geoff Hayes
le 24 Mai 2014
Modifié(e) : Geoff Hayes
le 24 Mai 2014
The code is missing the statement to write the data to the file using the file descriptor fid. Try instead:
fprintf('%d %.1f %.1f %3.1f\n', Data(row, :)); % to write to the console
fprintf(fid, '%d %.1f %.1f %3.1f\n', Data(row, :)); % to write to file
In the command window type help fprintf for details on this command.
2 commentaires
Image Analyst
le 25 Mai 2014
You can thank people by voting for their answer, and officially "Accepting" it. It will give them more privilege points to do things like edit posts and delete spam.
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!