MATLAB Answers

How do I create a text file in a loop?

36 views (last 30 days)
C G on 4 Apr 2018
Edited: C G on 5 Apr 2018
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.
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


Sign in to comment.

Accepted Answer

KSSV on 4 Apr 2018
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') ;
fclose(fid) ;


C G on 4 Apr 2018
Awesome! Thanks. Now, how do I create the loop part? For example, The next file would change the first variable to '11 01 30 18'.
KSSV on 4 Apr 2018
How and why your generating this data?
C G on 4 Apr 2018
Ultimately, I need to create a data file for a program that I am trying to automate. Currently, running the program through its user GUI requires 45 min per run and I have 408 runs to do, not including the inevitable times I will mess up the run.
Step 1: Create a loop in Matlab to write the control files... all 408 or 1600+ depending on the loop and how the first date is parsed.
Step 2: Run the program calling up each CONTROL file. Create the required text files needed for my analysis.
Step 3. Celebrate.

Sign in to comment.

More Answers (1)

C G on 5 Apr 2018
Edited: C G on 5 Apr 2018
Just in case there are any inquiring minds out there. This code is not the best, but it works. I am reading in an excel spreadsheet with varying ranges. I then create a loop that writes a text file with variable A and then a bunch of stuff, calling it file1.txt before moving on to the next variable. In this case, the first line is changed to variable B and it is called file2.txt.
A = xlsread(filename,sheet,xlRange1); %Or alternatively A= [1,2,3,4,5,6]
B = xlsread(filename,sheet,xlRange2);
C = xlsread(filename,sheet,xlRange3);
D = xlsread(filename,sheet,xlRange4);
F = {A,B,C,D};
for k = 1:numel(F)
[fid,msg] = fopen(sprintf('file%d.txt',k),'wt');
fprintf(fid,'%2.0f %2.0f %2.0f %2.0f %2.0f %2.0f',F{k}(1:6));
P={'';'3';'-71.166889 111.366531 10.0';'-71.166889 111.366531 2500.0';'-71.166889 111.366531
'./tdump10123018';} ;


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!