【问题标题】:MATLAB: Combining text filesMATLAB:组合文本文件
【发布时间】:2015-12-19 01:56:06
【问题描述】:

我想合并 'n' 个文本文件。

% Content of files  ('its is just dummy code')
file1.txt
19.09.2015 1 2 3 4

file2.txt
 20.09.20155 2 3 7

file3.txt
 21.09.2015 6 9 3 8

file4.txt %empty file

[FileNames,PathName] = uigetfile('*.txt','Select the txt file', 'MultiSelect', 'on');

Ofilestr='combinefile.txt'; % open an empty file to merge all
fileID = fopen(Ofilestr,'w');
fclose(fileID);
nbfiles =length(FileNames);
for it=1:nbfiles 
    file=FileNames{1,it};
% system('copy file+Ofilestr  Ofilestr') %% %% not working
     system(['copy' file+Ofilestr  'Ofilestr']) %% %% not working
end

任何想法。也是这个问题的另一种解决方案。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    奇怪的是,我在 StackOverflow 上找不到 好的 副本。

    通过您使用copy,我推测您正在寻找适用于 Windows 的解决方案。来自 Unix 背景,我会使用system(['cat ' FileNames{n} ' >> ' Ofilestr]);。 Windows 上的type 应该接近replacement for cat,因此以下内容应该可以实现您的目标。

    [FileNames, PathName] = uigetfile('*.txt', 'Select the txt file', 'MultiSelect', 'on');
    
    Ofilestr = 'combinefile.txt';
    fileID = fopen(Ofilestr,'w');
    fclose(fileID);
    
    for n = 1:numel(FileNames)
         system(['type ' FileNames{n} ' >> ' Ofilestr]);
    end
    

    对于 Unix 解决方案,只需将 type 更改为 cat


    如果你想要一个纯粹在 MATLAB 中的便携式版本,那么这就是我想要的

    [FileNames, PathName] = uigetfile('*.txt', 'Select the txt file', 'MultiSelect', 'on');
    
    Ofilestr = 'combinefile.txt';
    fileID = fopen(Ofilestr,'w');
    
    for n = 1:numel(FileNames)
        fwrite(fileID, fileread(FileNames{n}));
    end
    fclose(fileID);
    

    上述两种解决方案都会导致输出文件包含

    19.09.2015 1 2 3 4
    20.09.20155 2 3 7
    21.09.2015 6 9 3 8
    

    注意:我不建议使用

    Ofilestr = 'combinefile.txt';
    fileID = fopen(Ofilestr,'w');
    fclose(fileID);
    

    清空文件的内容。我会改用上面的代码来适应

    [FileNames, PathName] = uigetfile('*.txt', 'Select the txt file', 'MultiSelect', 'on');
    
    Ofilestr = 'combinefile.txt';
    
    if (numel(FileNames) > 1 || FileNames ~= 0)
        system(['type ' FileNames{1} ' > ' Ofilestr]);
        for n = 2:numel(FileNames)
             system(['type ' FileNames{n} ' >> ' Ofilestr]);
        end
    end
    

    【讨论】:

      【解决方案2】:

      那么这个怎么样?

      A = [];
      for ii = 1:length(files)
      
          % load new contents
          newA = load(files(ii).name, '-ascii');
      
          % concatenate horizontally
          A = [A newA];  %#ok
      
      end
      
      % save final output
      save('outputFile.txt', 'A')
      

      不管怎样,这个问题似乎已经有很多答案了

      这里还有一些其他需要考虑的尝试:

      http://www.mathworks.com/matlabcentral/answers/10916-combine-multiple-text-files-into-one-text-file

      http://www.mathworks.com/matlabcentral/answers/144284-simultaneously-merging-and-editing-multiple-text-files

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多