Writing an output to a text file

2 次查看(过去 30 天)
Damith
Damith 2014-5-24
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 个评论
Cedric
Cedric 2014-5-24
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
Image Analyst 2014-5-24
编辑:Image Analyst 2014-5-24
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?

请先登录,再进行评论。

回答(1 个)

Geoff Hayes
Geoff Hayes 2014-5-24
编辑:Geoff Hayes 2014-5-24
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 个评论
Image Analyst
Image Analyst 2014-5-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by