how to save the result of each iteration of 'for' loop in a text file without replacing the previous results in the text file????

7 ビュー (過去 30 日間)
ow t
  2 件のコメント
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2018 年 12 月 8 日
Here is the sample code
for ii = 1:Ne
enc_x(ii+1) = r*enc_x(ii)*(1 - enc_x(ii));
enc_y(ii+1) = r*enc_y(ii)*(1 - enc_y(ii));
fid=fopen('my1.bin','w');
if(enc_x(ii)>enc_y(ii))
iwant(ii)=0;
else
iwant(ii)=1;
end
fprintf(fid,'%d \n',iwant);
fclose(fid);
madhan ravi
madhan ravi 2018 年 12 月 21 日
編集済み: madhan ravi 2018 年 12 月 21 日
Attach the sample data and your desired file output as a short example of what you mean

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

回答 (4 件)

Arvind Sathyanarayanan
Arvind Sathyanarayanan 2018 年 12 月 7 日
Have you tried the fprintf command?
  1 件のコメント
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2018 年 12 月 8 日
yes but the last value is shown. Here is the sample code
for ii = 1:Ne
enc_x(ii+1) = r*enc_x(ii)*(1 - enc_x(ii));
enc_y(ii+1) = r*enc_y(ii)*(1 - enc_y(ii));
fid=fopen('my1.bin','w');
if(enc_x(ii)>enc_y(ii))
iwant(ii)=0;
else
iwant(ii)=1;
end
fprintf(fid,'%d \n',iwant);
fclose(fid);

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


KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2018 年 12 月 8 日
its not my1.bin ...it is my1.txt file

Arvind Sathyanarayanan
Arvind Sathyanarayanan 2018 年 12 月 10 日
Please see my code below. I open file before my for loop and close it after the end of the for loop.
fid = fopen('Test.txt','w');
for i = 1:100
if mod(i,2)
fprintf(fid,'%d\r',i);
end
end
fclose(fid);
  7 件のコメント
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2018 年 12 月 21 日
fid=fopen('test.txt','w');
x(1)=0.012;
for ii = 1:10
x(ii+1) = r*x(ii)*(1 - x(ii));
y(ii+1) = r*y(ii)*(1 - y(ii));
if (x(ii)>y(ii))
i_decrypt(ii)=0;
else
i_decrypt(ii)=1;
end
fprintf(fid,'%d\r\n',i_decrypt);
end
fclose(fid);
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2018 年 12 月 21 日
test.txt file should contain the outputs of the each iteration as shown i.e
for iteration one:::::12345
for iteration two::::23456
for iteration three:::::34567
for iteration four:::::23344
for iteration five:::::66777
but when above program is executed the test file contains the outputs as shown
1234523456345672334466777...................

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


Elijah Smith
Elijah Smith 2018 年 12 月 18 日
you must open the file with "append" permissions not "write" permissions like so:
fid = fopen(my1.txt, 'a'); %this will not erase what is currently in the file
  2 件のコメント
Elijah Smith
Elijah Smith 2018 年 12 月 18 日
you opened it like so:
fid = fopen(my1.txt, 'w');
this will write over what is currently in the file.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by