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

1 view (last 30 days)
diana sandra
diana sandra on 3 Apr 2017
Commented: Jan on 19 Sep 2017
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 Comment
Jan
Jan on 19 Sep 2017
@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.

Sign in to comment.

Answers (1)

Joseph Cheng
Joseph Cheng on 3 Apr 2017
Edited: Stephen23 on 3 Apr 2017
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 Comments
Jan
Jan on 6 Apr 2017
@karen: If it works, please accept this answer to show, that the problem is solved. Thanks.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!