fprintf within a for / if environment with crazy behaviour

조회 수: 1(최근 30일)
Harald von der Osten
Harald von der Osten 2022년 6월 9일
댓글: Harald von der Osten 2022년 6월 9일
Using fprintf in a for / if environment (fileID = correct.XP0000020_0026.dat):
if ProfilKonst == 'X'
for i=1:length(x)
if x(i) == koordinate(1)
fprintf(fileID,'%.2f %.2f %.2f\n',[x(i) y(i) z(i)]);
end
end
end
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
Stephen23 2022년 6월 9일
편집: Stephen23 2022년 6월 9일
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)])
fclose(fid);
  댓글 수: 4
Harald von der Osten
Harald von der Osten 2022년 6월 9일
okay okay, now I can see my mistake. Thanks again, Stephen

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by