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

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