MATLAB: For loop, write to text file, append, sprintf, fprintf

appendfor loopfprintfsprintftext file

Hi, I want to make a for_loop that adds lines to a text file.
first loop: direction_lights_1 = [1 2 3] second loop: direction_lights_2 = [4 5 6] … fifth loop: direction_lights_5 = [13 14 15]
My code so far looks like this:
fileID = fopen(sprintf('textfile.txt','w'); %here I create the text file
fprintf(fileID, '%f %f %f \n', direction_lights); %here I print the 3 numbers and then go to a new line
fclose(fileID);
Now I want the program to append lines to the existing text file. It should look somewhat like this:
for i=1:5 %obviously open the file again
fprintf(fileID, '%f %f %f \n', direction_lights_i.'a');
end
I can't figure out how to make it work. Hope it is clear what I'm trying to achieve. Thanks for your help! J

Best Answer

fileID = fopen('textfile.txt', 'a');
for i=1:5
fprintf(fileID, '%f %f %f \n', direction_lights_i);
end
fclose(fileID);
The 'a' belongs to the fopen command, not to fprintf.