MATLAB: Merging files using for loop

merge text file

I am trying to merge 7 text files into 1 using system ('copy 090615.txt+090616.txt+090617.txt+090618.txt+090619.txt+090619.txt+090620.txt+090621.txt New.txt'); How can I do the same thing using a for loop. Thanks

Best Answer

The above method should work.
However, instead of copying the content into a string ( which might be HUGE for several files ), try using the flag 'a' (append) instead of 'w' (write, will start at the beginning of the file again) when opening the file, i.e.
fid = fopen(newFile, 'a');
This way, you can read the files one at a time, and write the content to a single file.
PS: If the file is not empty in the beginning, but you don't want to keep the old content, you need to open it with "w" for k == 1, and then use "a".