Write multiple columns of data in a single text file

I want to generate weather file which is in the format using MATLAB
year month date rainfall tmax tmin 2006 1 1 2 23.56 7.25
I can generate this by the following command for only one line of data. The files contains 34698 lines. I can run only the first line. Now, as far i think I have to run a loop to get all the data for each latitude and longitude in a text file. Please help me in this regard.
if true
% rain = xlsread('pr_WAS-44_ICHEC-EC-EARTH_rcp85_r12i1p1_SMHI-RCA4_v2_day .xlsx',1,'D1:BN34699');
tasmax = xlsread('rcp85_tasmax_smhi_ecearth.xlsx',1,'D1:BN34699');
tasmin = xlsread('tasmin_rcp85_smhi.xlsx',1,'D1:BN34699');
lat = xlsread('lat.xlsx',1);
lon = xlsread('long.xlsx',1);
date = xlsread('date.xlsx',1);
tmax = tasmax-273; %conversion kelvin to celcious
tmin=tasmin-273; %conversion kelvin to celcious
outfile=sprintf('%6.2f_%6.2f.txt',lat(1),lon(1));
fid=fopen(outfile,'wt');
fprintf(fid,'%d %d %d %5.2f %5.2f %5.2f\n',[date(1,1) date(1,2) date(1,3) rain(1,1) tmax(1,1) tmin(1,1)]);
fclose(fid);
disp([outfile 'created']);
end

Answers (1)

data_to_write = [date(:,[1 2 3]) rain(:,1) tmax(:,1) tmin(:,1)];
fid=fopen(outfile,'wt');
fprintf(fid,'%d %d %d %5.2f %5.2f %5.2f\n',data_to_write.');
fclose(fid);

Answered:

on 9 Jan 2024

Community Treasure Hunt

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

Start Hunting!