MATLAB: How to create a text file in a loop

create text fileloopwrite text file

I am sure this has been asked before, but searching for it has lead me nowhere.
I need to create a text file and then have the code create copies of that file after changing a few things.
This is what I need the file to look like:
This is my code so far, but I can't seem to get the file to even open. I haven't attempted the loop part. I have placed the "CONTROL.txt" file in the MATLAB directory, but still nothing.
Any help would be appreciated.
fopen('CONTROL.txt','r');
fprintf('12 12 30 18 \n'); % This is the date line
fprintf('3 \n'); % This is the number of starting locations
fprint('-71.166889 111.366531 10.0 \n')% This is a list of starting locations, including elevation (agl)
fprint('-71.166889 111.366531 2500.0 \n') %This is the second starting location.
fprint('-71.166889 111.366531 5000.0 \n') % This is a third starting location
fprintf('-240 \n') % Number of hours for back trajectory. Needs to have a negative sign
fprint('0 \n')% Not sure what this is, just leave it as 0
fprint('30000.0 \n') % Height of model, leave at 30000.0
fprint('5 \n')% Number of meterological data files, possibly.
fprint('C:/hysplit4/MetData/gdas1.dec10.w5 \n') % The first metdata file.
fprint('C:/hysplit4/MetData/gdas1.dec10.w1 \n') % The second net data file... and so on
fprint('C:/hysplit4/MetData/gdas1.dec10.w2 \n')
fprint('C:/hysplit4/MetData/gdas1.dec10.w3 \n')
fprint('C:/hysplit4/MetData/gdas1.dec10.w4 \n')
fprintf('./tdump10123018 \n')% Possibly the name of the tdump files
fclose(fid);

Best Answer

You need not to write line by line....make your entire content to a cell array and write this cell into text file, in a single step. Check the below example code:
A = {'10 12 30 18' ; '3' ; '-71.166889 111.366531 10.0' ; '-240' ; '0' ;
'C:/hysploit4/MetData/gdas1.dec10.w5'} ; ;
% Write to file
fid = fopen('data.txt','wt') ;
fprintf(fid,'%s\n',A{:});
fclose(fid) ;