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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Rik
Rik il 20 Mag 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 Commenti
Sanket
Sanket il 3 Ott 2022
I understood your point. But as per the format specifier, the resulting csv file should have two columns. If you wiil check, the very first cell of the second file is missing. What could be the format specification for this?
Stephen23
Stephen23 il 3 Ott 2022
Modificato: Stephen23 il 3 Ott 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by