Append to Text File with For Loop

I am trying to append to a text file using a for loop. Here is a generalized version of my script:
for 1:length(variable)
if isempty(dir(file_name)) == 1
fid = fopen(strcat(pwd,'\',file_name),'w');
fprintf(fid,strcat(repmat('%s\t',1,5),'\n'),...
'Daily Directory',...
'Test Directory',...
'Data File',...
'Position',...
'Maximum');
fclose(fid);
else
end
fid = fopen(strcat(pwd,'\',file_name),'a');
fprintf(fid,strcat(repmat('%s\t',1,5),'\n'),...
daily_directory,...
test_directory,...
file,...
position_id,...
vel_max);
fclose(fid);
end
When I run my script, second fprintf statement appears to overwrite the text that was produced in the previous step through this for loop. For the line
fid = fopen(strcat(pwd,'\',file_name),'a');
I have tried these options: 'a', 'at', and 'a+', but the text continues to be overwritten. Can someone please tell me how to fix my code, such that the text in my text file is appended to instead of being overwritten?
Thanks!

5 Kommentare

Voss
Voss am 17 Jun. 2022
Bearbeitet: Voss am 17 Jun. 2022
It doesn't appear to be a problem with the write mode/permissions:
file_name = 'test.txt';
% opening and writing the first time:
fid = fopen(strcat(pwd,filesep(),file_name),'w'); % using filesep() instead of '\', but...
% fid = fopen(fullfile(pwd(),file_name),'w'); % <-- ...using fullfile is preferred
fprintf(fid,strcat(repmat('%s\t',1,5),'\n'),...
'Daily Directory',...
'Test Directory',...
'Data File',...
'Position',...
'Maximum');
fclose(fid);
type test.txt % check the contents so far
Daily Directory Test Directory Data File Position Maximum
% opening and appending:
fid = fopen(strcat(pwd,filesep(),file_name),'a');
% fid = fopen(fullfile(pwd(),file_name),'a');
fprintf(fid,strcat(repmat('%s\t',1,5),'\n'),...
'some',...
'random',...
'(or',...
'non-random)',...
'data');
fclose(fid);
type test.txt % check the contents now... looks like it has everything
Daily Directory Test Directory Data File Position Maximum some random (or non-random) data
It may be necessary to see the actual code that's running, in order to have a chance at debugging it.
Rik
Rik am 17 Jun. 2022
Did you make sure the first fprintf actually runs?
Allen Hammack
Allen Hammack am 18 Jun. 2022
@Voss You are correct about the problem not being in the section of script I included in my question. I was overwriting my entire file each time through my for loop. I've corrected that problem, and everything is working correctly now. Thank you!
Also, thank you for the suggestions with the fullfile command! I've incorprated that into my script.
I don't see how to accept your answer to make sure this question is marked as complete.
Voss
Voss am 18 Jun. 2022
@Allen Hammack: Regarding accepting my answer: Well, it was a comment, as opposed to an answer per se, so there's no way to accept it (and it didn't address the actual problem, as it turns out).
In any case, I'm glad you got it figured out!
Image Analyst
Image Analyst am 19 Jun. 2022
@Voss can make an answer with the same solution below. Then it could be accepted.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Gefragt:

am 17 Jun. 2022

Kommentiert:

am 19 Jun. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by