combining text files from separate folders to one text file using for loop

3 views (last 30 days)
I have two folders namely 'tmax' and 'tmin'. I have text files having names tmax_1,tmax_2,tmax_3 and tmin_1, tmin_2,tmin_3 located in the folders tmax and tmin, respectively. I want to combine tmax_1 and tmin_1 having single column from the two folders to create a new text file namely tmean_1 consisting of two columns. Similarly, I want to combine the remaining two files using for loop.

Accepted Answer

Rik
Rik on 20 May 2020
for n_file=1:3%or use dir() to find which files you have
fname_max=sprintf('tmax_%d',n_file);
data_max=___%read data to vector
fname_min=sprintf('tmin_%d',n_file);
data_min=___%read data to vector
data_mean=[data_min data_max];
fname_min=sprintf('tmean_%d',n_file);
%write data to file (e.g. with fprintf)
end
  16 Comments
Stephen23
Stephen23 on 3 Oct 2022
Edited: Stephen23 on 3 Oct 2022
"What could be the format specification for this?"
You need to specify the delimiter character in the format specification, e.g.:
'%f,%f\n'
^ delimiter comma
Note that you need to FCLOSE those files, otherwise you could cause MATLAB and your OS to crash.

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by