fprintf within a for / if environment with crazy behaviour
1 view (last 30 days)
Harald von der Osten on 9 Jun 2022
Using fprintf in a for / if environment (fileID = correct.XP0000020_0026.dat):
if ProfilKonst == 'X'
if x(i) == koordinate(1)
fprintf(fileID,'%.2f %.2f %.2f\n',[x(i) y(i) z(i)]);
the file 'correct.XP0000020_0026.dat' is empty, and the output on the console is:
correct.XP0000020_0026.datcorrect.XP0000020_0026.datcorrect.XP0000020_0026.datcorrect.XP0000020_0026.dat..........and so on
But when I use
fprintf('%.2f %.2f %.2f\n',[x(i) y(i) z(i)]);
everything is fine.
The file is opened with fopen(fileID,'wt'). I also tried to move fprintf in a function, but with the same result...
What is wrong? Thanks a lot.
Stephen23 on 9 Jun 2022
Edited: Stephen23 on 9 Jun 2022
The file is opened with fopen(fileID,'wt')"
Perhaps, but in any case, you did not provide the file identifier to FPRINTF.
"What is wrong?"
You did not provide a valid file identifier to FPRINTF. You are doing this:
fileID = 'correct.XP0000020_0026.dat';
fprintf(fileID,'%.2f %.2f %.2f\n',[x(i) y(i) z(i)])
% ^^^^^^ prints the filename
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ these are ignored
whereas what you need to do is get the identifier from FOPEN and use that:
fid = fopen(correct.XP0000020_0026.dat','wt');
fprintf(fid,'%.2f %.2f %.2f\n',[x(i) y(i) z(i)])