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

조회 수: 1 (최근 30일)
Sanket
Sanket 2020년 5월 20일
편집: Stephen23 2022년 10월 3일
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