How do I create a text file in a loop?

조회 수: 15 (최근 30일)
C G
C G 2018년 4월 4일
편집: C G 2018년 4월 5일
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);

채택된 답변

KSSV
KSSV 2018년 4월 4일
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) ;
  댓글 수: 3
KSSV
KSSV 2018년 4월 4일
How and why your generating this data?
C G
C G 2018년 4월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

C G
C G 2018년 4월 5일
편집: C G 2018년 4월 5일
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');
assert(fid>=3,msg)
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
5000.0';'-240';'0';'30000.0';'5';'C:/hysplit4/MetData/gdas1.dec10.w5';
'C:/hysplit4/MetData/gdas1.dec10.w1';'C:/hysplit4/MetData/gdas1.dec10.w2';'C:/hysplit4/MetData/gdas1.dec10.w3';'C:/hysplit4/MetData/gdas1.dec10.w4';
'./tdump10123018';} ;
fprintf(fid,'%s\n',P{:});
fclose(fid);
end

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by