How do I export the calculations of a textfile?

5 ビュー (過去 30 日間)
Eneru Y
Eneru Y 2017 年 3 月 22 日
コメント済み: Walter Roberson 2017 年 3 月 22 日
I have to export to a file all the values of variables X and Y.
A = 10:0.1:100;
B = 100:1:1000;
C = 1:0.01:10;
X = A .* B;
Y = (A ./ B) .* C;
i.e. the X and Y values for each value of A, B and C. I've tried with
M = [ X ; Y ];
nomefile = fopen ( 'FILE.txt' , 'a');
fprintf ( nomefile , '%16.15E %16.15E\r\n' , M );
fclose (nomefile);
but I got a mess. I was supposed to get a file with 7.29E+08 lines, but I got a much smaller file with wrong values. Thank you!
  7 件のコメント
Stephen23
Stephen23 2017 年 3 月 22 日
編集済み: Stephen23 2017 年 3 月 22 日
"Thus X column = 811,801 rows and Y column = 731,432,701 rows"
Nope, not even close:
>> size(X)
ans =
1 901
>> size(Y)
ans =
1 901
If you want matrix multiplication then you need to use the correct operator. If you want all permutations then use bsxfun, although you will likely run out of memory.
Walter Roberson
Walter Roberson 2017 年 3 月 22 日
Using ndgrid or bsxfun runs out of memory on my machine.
Also, 901*901 is odd, so it is not clear how one would write it out as two columns? Two in which order? Should the last of the rows have only one entry?

サインインしてコメントする。

回答 (1 件)

Eneru Y
Eneru Y 2017 年 3 月 22 日
I think I found the solution:
for A = 10:0.1:100
for B = 100:1:1000
for C = 1:0.01:10
X = A * B;
Y = (A / B) * C;
U = [ X ; Y ];
fid = fopen('FILE.txt','a');
fprintf(fid,'%16.15E %16.15E\r\n', U );
fclose(fid);
end
end
end
This way it works. Thanks anyway!
  2 件のコメント
Stephen23
Stephen23 2017 年 3 月 22 日
編集済み: Stephen23 2017 年 3 月 22 日
It would be much better to open the file before and after the loop, use w to write to a cleared file, and use the t option (because this handles newlines correctly):
fid = fopen('FILE.txt','wt');
for ...
....
fprintf(fid,'%16.15E %16.15E\n', U );
end
fclose(fid);
Eneru Y
Eneru Y 2017 年 3 月 22 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by