1:  %% merge.m
   2:  %%%%Main程序%%%%%%
   3:  %%%%%%
   4:  %%%%%%本程序合并完各个子文件夹中的txt到主文件目录下,并且合并的文件以子文件夹名字命名
   5:  %%%%%%同时,每次合并时,如果主文件夹已存在某一个子文件夹名字A的txt文件,那么此次合并,将把当前
   6:  %%%%%%这个子文件夹A中所有的txt文件追加到子文件夹A.txt文件的末尾处。
   7:  clear;
   8:  clc; 
   9:  % MainFolder='D:\Master-FTP\磁流变阻尼器实验\'; %%设置主目录,注意结尾要有个'\'
  10:  MainFolder='E:\txt文件夹\';
  11:  dirOutput=dir(fullfile(MainFolder)); %%获取主目录下所有文件夹与文件
  12:   
  13:  num=size(dirOutput);%%获取数量
  14:  for i=1:num(1)
  15:      if dirOutput(i,1).isdir == 1 %判断是否为文件夹
  16:          a1=~ strcmp(dirOutput(i,1).name,'.'); %%排除'.'和'..'的文件夹
  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('合并完成');

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

   1:  %% TAllFileInAFolder.m
   2:  function yyy=TAllFileInAFolder(MainFolderName,SubFolderName) %%合并子文件夹下所有txt,存到主文件夹下,以子文件夹命名
   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');%%保存路径
   9:  fid2=fopen(file2,'a');
  10:  for i=1:num(1)
  11:      file1=strcat(fileFolder,'\',fileNames{i,1});%%读取的文件
  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:      %fprintf(fid2,'\r\n')表示在写完每个txt文件之后,写入一个回车换行符,得到下一个txt从新的一行开始写
  19:      fprintf(fid2,'\r\n'); 
  20:  end
  21:  fclose(fid2);
  22:  yyy=1;

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

主文件夹下的子文件夹:

某一子文件夹下的txt数据文件:

执行合并之后:

多次执行合并之后从文件大小可知其是子文件夹中txt数据是追加到已合并的子文件夹.txt中的。

<本文实现源码参考源>http://wheartbeating.blog.163.com/blog/static/2044620422013102811336829/

matlab批量合并txt文件的更多相关文章

  1. matlab批量读取一个文件夹里类似命名的mat文件

    参考网址: Matlab读取同一路径下多个txt或mat文件总结 matlab 批量读取数据文件.mat .dat 整理:matlab批量读入数据文件的方法 首先命名方式体现在只是名字里数字有变化,其 ...

  2. 使用ffmpeg批量合并flv文件

    title: 使用ffmpeg批量合并flv文件 toc: false date: 2018-10-14 16:08:19 categories: methods tags: ffmpeg flv 使 ...

  3. R语言笔记004——R批量读取txt文件

    R批量读取txt文件 本文数据,代码都是参考的是大音如霜公众号,只是自己跟着做了一遍. path<-'C:\\Users\\Administrator\\Desktop\\docs' docs& ...

  4. python批量创建txt文件,以demo.txt内的内容为文件名

    #批量创建txt文件import sys,osa=open("demo.txt")n=0aList=[]for line in a.readlines(): aList.appen ...

  5. matlab之导入txt文件并取其中一列数据

    1.我想导入下面这个文件的内容,并且获取这个文件中的两列数据. 2.首先确保Matlab当前所在文件夹为txt文件所在文件夹,然后命令行执行: X = load('2019_03_21_08_59_0 ...

  6. MATLAB:读取txt文件中物体的三维坐标,显示三维模型

    在MATLAB中建立一个脚本show3Dtxt.m文件,编写代码: clear; %%read 3D data fileID= fopen('E:\博士\深度学习与三维重建\代码实现\voxel_gr ...

  7. python批量读取txt文件为DataFrame

    我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...

  8. 使用Python批量合并PDF文件(带书签功能)

    网上找了几个合并pdf的软件,发现不是很好用,一般都没有添加书签的功能. 又去找了下python合并pdf的脚本,发现也没有添加书签的功能的. 于是自己动手编写了一个小工具,使用了PyPDF2. 下面 ...

  9. MATLAB的SAVE命令动态批量保存TXT文件

    1.使用save(): for i=1:6 str=[num2str(i),’.txt’]; m=[1 2; 3 4]; save(str,’m’,’-ascii’);%注意m的单引号,一定记得加上, ...

随机推荐

  1. iOS 中的 NSTimer

    iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...

  2. 8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

  3. [转]Designing a User Interface

    UI design can be divided into three essential elements : functionality, aesthetics, and performance. ...

  4. 捡火柴的Nova君(n个线段相交问题)

    题目来源:https://biancheng.love/contest-ng/index.html#/41/problems 捡火柴的Nova君 题目描述 南方没暖气,怕冷的的宝宝们只能用火柴取暖.然 ...

  5. androidannotation study(1)---Activity, Fragment,Custom Class & Custom View

    androidannotation 是github上的一个开源项目. 主要是注解机制,可以改善android写代码的效率. Activity 使用 1.@EActivity 注解 可想而知,servi ...

  6. C# List中随机获取N个字符

    static void Main(string[] args) { List<string> strList = new List<string>(); ; i <= ; ...

  7. Effective Java 27 Favor generic methods

    Static utility methods are particularly good candidates for generification. The type parameter list, ...

  8. git提交报错

    Error occurred computing Git commit diffsMissing unknown 0000000000000000000000000000000000000000 co ...

  9. phpcmsv9 标题颜色显示问题

    在解决标题颜色问题之前首先要注意到 标题字段为title,副标题为fu_title. 如果一个文章想在首页推荐,又想在栏目首页推荐,并且这两个推荐位置的标题长度不一样,那只能用副标题区别,这样就可以在 ...

  10. Codeforces 13C Sequence --DP+离散化

    题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...