fprintf within a for / if environment with crazy behaviour

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

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

Testing:
x = 11; % totally irrelevant
y = 22; % ditto
z = 33; % ditto
idx = 1; % ditto
fileID = 'correct.XP0000020_0026.dat';
fprintf(fileID,'%.2f %.2f %.2f\n',[x(idx) y(idx) z(idx)])
correct.XP0000020_0026.dat
No file ID -> prints to the command window, repeated in a loop.
thanks a lot for your help.
There is still something I don't understand right now:
With my code, fprintf should be provided with fileID :
tline='XP0000020_0026.DAT';
fileID=append('correct.',tline);
fopen(fileID,'wt');
but, as stated, it doesn't work. With your line:
fileID = fopen('correct.XP0000020_0026.dat','wt');
it works.
Well, 'tline' should get its value later on out of a listing file, consisting of nearly 900 filenames, so I would like to know why the code doesn't work...
"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);
okay okay, now I can see my mistake. Thanks again, Stephen

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!