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

1 view (last 30 days)
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
Joseph Cheng
Joseph Cheng on 4 Apr 2017
Edited: Joseph Cheng on 4 Apr 2017
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 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!