matlab batch merge txt file

   1:   %% merge.m
   2:   %%%% Main program %%%%%%
   3:  %%%%%%
   4:   %%%%%% the Program End Merge the individual subfolders to the main txt file directory, and the combined file to a subfolder named
   5:   %%%%%% the same time, each time the merger, if the master folder is a sub-folder name A txt file exists, then the merger, will present
   6:   %%%%%% this all subfolders A txt file is appended to the end of the subfolders A.txt file.
   7:  clear;
   8:  clc; 
   . 9:   % MainFolder = "D: \ the FTP-Master \ experimental magnetorheological damper \ '; %% set the main directory, there must be noted that the end of the' \ '
  10:   MainFolder = 'E: \ folder TXT \ ';
  . 11:   dirOutput the dir = (fullfile (MainFolder)); %% obtain all folders and files under the home directory
  12:   
  13 is:   NUM = size (dirOutput); %% Get Number
  14:  for i=1:num(1)
  15:       IF dirOutput (I,. 1). 1% .isdir == determines whether folder
  16:           A1 = ~ strcmp ( 'dirOutput (I,. 1) .name, '); %% excluded ' . And' ' .. ' folder
  17:          a2=~ strcmp(dirOutput(i,1).name,'..');
  18:          if  a1 &&   a2;
  19:              dirOutput(i,1).name;
  20:          y=TAllFileInAFolder(MainFolder,dirOutput(i,1).name);
  21:          end
  22:      end
  23:  end
  24:   DISP ( 'merger');

 

   1:  %% TAllFileInAFolder.m
   2:   function = yyy TAllFileInAFolder (MainFolderName, SubFolderName) %% merge all subfolders txt, saved in the home folder, subfolder named
   3:  fileFolder=fullfile(MainFolderName,SubFolderName);
   4:  dirOutput=dir(fullfile(fileFolder,'*.txt'));
   5:  fileNames={dirOutput.name}';
   6:  num=size(dirOutput);
   7:   
   . 8:   file2 = strcat (fileFolder, '\ .. \ ', SubFolderName, ' .txt '); %% save path
   9:  fid2=fopen(file2,'a');
  10:  for i=1:num(1)
  . 11:       file1 = strcat (fileFolder, '\ ', I fileNames {,}. 1); read file %%
  12:      fid=fopen(file1,'r');
  13:      %txt=fscanf(fid,'%s');
  14:      txt=fread(fid,inf);
  15:      fclose(fid);
  16:      %fprintf(fid2,'%s',txt);
  17:      fwrite(fid2,txt);
  18 is:       % fprintf (FID2, '\ R & lt \ n- ') each represents a txt file written after writing a carriage returns to give a txt next to start writing a new line
  19:      fprintf(fid2,'\r\n'); 
  20:  end
  21:  fclose(fid2);
  22:  yyy=1;

Subfolder of the home folder under the folder:

image

A sub-folder txt data files:

image

After the implementation of the merger:

image

After repeating the merger is apparent from the file size of its subfolders txt data is appended to the merged sub-folders in .txt.

image

<Reference herein to realize source Source> http://wheartbeating.blog.163.com/blog/static/2044620422013102811336829/

Reproduced in: https: //www.cnblogs.com/AI-Algorithms/p/3744772.html

Guess you like

Origin blog.csdn.net/weixin_34161029/article/details/94506329