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

1 次查看(过去 30 天)
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.

采纳的回答

Rik
Rik 2020-5-20
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 个评论
Sanket
Sanket 2022-10-3
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 2022-10-3
编辑:Stephen23 2022-10-3
"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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by