Filter löschen
Filter löschen

How to solve override writing file problem?

1 Ansicht (letzte 30 Tage)
Jason
Jason am 23 Mär. 2016
Kommentiert: Star Strider am 23 Mär. 2016
I have try-catch in my code, when I catch the error, I write the error information to a file, but it always override the previous error information, how to do to avoid this problem? I want to append the next error information to the previous error information.
for p=3:10
...
Day = fdname(p).name;
try
...
catch
fid = fopen('output-2016.err', 'wt');
fprintf(fid, 'Inconsistent data in %s, skipped.\n', p);
fprintf(fid, 'Inconsistent data in %s, skipped.\n', Day);
fclose(fid);
end

Akzeptierte Antwort

Star Strider
Star Strider am 23 Mär. 2016
Bearbeitet: Star Strider am 23 Mär. 2016
I would put the fopen and fclose calls outside the loop:
fid = fopen('output-2016.err', 'wt');
for p=3:10
...
Day = fdname(p).name;
try
...
catch
fprintf(fid, 'Inconsistent data in %s, skipped.\n', p);
fprintf(fid, 'Inconsistent data in %s, skipped.\n', Day);
end
. . . CODE . . .
end
fclose(fid);
  2 Kommentare
Jason
Jason am 23 Mär. 2016
awesome. thanks
Star Strider
Star Strider am 23 Mär. 2016
My pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by