フィルターのクリア

Writing an output to a text file

2 ビュー (過去 30 日間)
Damith
Damith 2014 年 5 月 24 日
コメント済み: Image Analyst 2014 年 5 月 25 日
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 件のコメント
Damith
Damith 2014 年 5 月 25 日
Thanks.
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 Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by