I think, instead of the code creating "only the last file", it is overwritting the same file, so you end up with only one file.
Try renaming the file based on the iteration number. Something like this:
for i = 1: n_iterations
filename = strcat('file_id_',num2str(i));
Hope this helps.