fprintf within a for / if environment with crazy behaviour

1 view (last 30 days)
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.

Accepted Answer

Stephen23
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)])
fclose(fid);
  4 Comments
Stephen23
Stephen23 on 9 Jun 2022
Edited: Stephen23 on 9 Jun 2022
"With my code, fprintf should be provided with fileID"
The misleading name you gave that variable is totally irrelevant: you are NOT providing FPRINTF with the file identifier of an open file. You are still providing FPRINTF with a character vector, just like when you asked your question.
"so I would like to know why the code doesn't work... "
Because you are still providing FPRINTF with a character vector ( which I already explained in my answer):
tline='XP0000020_0026.DAT';
fileID=append('correct.',tline) % is this a file identifier or a character vector?
fileID = 'correct.XP0000020_0026.DAT'
Is the badly-named fileID the identifier to a file opened by FOPEN? No, it is not. It is just a character vector.
Your code does open a file (and implicitly there is also an identifier somewhere in there):
fopen(fileID,'wt'); % what do you allocate the output to? !!!! NOTHING !!!!
But you do not allocate FOPEN's output to anything so the actual file identifier is simply discarded. So that open file is basically ... useless. And even worse, you then have no way to FCLOSE that specific file.
If you want the file ID of an open file then you will need the output from FOPEN:
tline = 'XP0000020_0026.DAT';
thisIsNotFileID = append('correct.',tline);
fid = fopen(thisIsNotFileID,'wt'); % <-------------- !!!! FILE ID HERE !!!!
fprintf(fid,..)
fclose(fid);

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!