Loop to generate a folder and to join files in .txt format

조회 수: 1 (최근 30일)
diana sandra
diana sandra 2017년 4월 3일
댓글: Jan 2017년 9월 19일
Hi
I would like to join the information of two folders. Each folder has 58 files with one column in .txt format. The first folder has maximum temperature records TMX_M1_1960-2009 and its files have the following nomenclature:
tmx_m1_1960-2009_0 tmx_m1_1960-2009_1 . . . tmx_m1_1960-2009_57
The second folder has minimun temperature records TMX_M1_1960-2009 and its files have the following nomenclature
tmn_m1_1960-2009_0 tmn_m1_1960-2009_1 . . . tmn_m1_1960-2009_57
I would like to generate a forlder: TMX_TMN_M1_1960-2009, which contains 58 files with the same nomenclature:
tmx_tmn_m1_1960-2009_0 tmx_tmn_m1_1960-2009_1 . . tmx_tmn_m1_1960-2009_57
The two columns of the generated files (maximum temperature and minimum temperature) must be separated with a comma (,) and have a .txt extension
Thanking in advance for any help.
[REVERTED, author has edited away the question, Jan]
  댓글 수: 1
Jan
Jan 2017년 9월 19일
@karen: Please do not delete the question after someone has spent the time to post an answer. This is a public forum and the voluntary users agree to invest their work, because the solutions are shared in public. As soon as you edit away the test of the question, the answer becomes meaningless and this is not respectful.
Does Joseph's answer help so solve the problem? Then please accept it.

댓글을 달려면 로그인하십시오.

답변 (1개)

Joseph Cheng
Joseph Cheng 2017년 4월 3일
편집: Stephen23 2017년 4월 3일
without generating some dummy files here is how i see your loop and joining of the two files. you'll have to modify it depending on the actual file structure but it'll atleast get you started
tmxfolder = %insert folder path for your tmx files
tmnfolder = %insert folder path for your tmn files
tmxtmnfolder = %insert folder path for your output files
tmxfiles = dir(fullfile(tmxfolder,'*.txt'));
for ind = 1:numel(tmxfiles)
tmxdata = dlmread(fullfile(tmxfolder,tmxfiles(ind).name));
tmndata = dlmread(fullfile(tmnfolder,['tmn' tmxfiles(ind).name(4:end)));
fid = fopen(fullfile(tmxtmnfolder,['tmx_tmn' tmxfiles(ind).name(4:end))],'wt');
fprintf(fid,'%d,%d\r\n',[tmxdata tmndata]);
fclose(fid)
end
  댓글 수: 2
Joseph Cheng
Joseph Cheng 2017년 4월 4일
편집: Joseph Cheng 2017년 4월 4일
if you investigate it you can see that the data is being taken from first value and then sequentially placed in then. mistakenly i forgot that matlab does column wise not row wise operations. Instead of it being row(1) and the 2 columns with new lines for each new row it was grabbing data from column 1, two rows at a time.
if you perform a quick debug of the code or looked at the fprintf help you'll see that
fprintf(fid,'%d,%d\r\n',[tmxdata tmndata]');
should work or..... with the implementation of dlmread() you could have made the file with dlmwrite()
Jan
Jan 2017년 4월 6일
@karen: If it works, please accept this answer to show, that the problem is solved. Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by