简体   繁体   English

根据 Matlab 中的文件名加载.csv 文件

[英]Loading .csv files based on file name in Matlab

I have a large number of csv files which are named sequentially based on date in the same folder.我有大量 csv 文件,这些文件在同一文件夹中根据日期顺序命名。 example name (d_log_yyyymmddhhmmss).示例名称 (d_log_yyyymmddhhmmss)。 All files have the same number of columns and headers and ultimately I am concatanating.所有文件都有相同数量的列和标题,最终我正在连接。

I am loading them using the following which seems to be fast and efficient:.我正在使用以下内容加载它们,这似乎是快速有效的:。 However, this works for limited files and as I have large amounts I want to split them in cells based on the date they include in their name.但是,这适用于有限的文件,并且由于我有大量文件,我想根据它们名称中包含的日期将它们拆分为单元格。 For example within a month it would create 30 cell arrays with individual files and 30 concatanated files.例如,在一个月内,它将创建 30 个单元 arrays,其中包含单个文件和 30 个串联文件。 Any help appreciated.任何帮助表示赞赏。

d = uigetdir();
 filePattern = fullfile (d, '*.csv');
 file = dir (filePattern);
Data =cell (1, numel(file));
 for k=1 : numel(file)
     filename = file(k).name;
     fullfilename = fullfile (d, filename);
     Tables {k} = readtable (fullfilename);
     fprintf ('read file %s\n', fullfilename);
     
 end

bigdata = vertcat (Data{:});

EDIT编辑

    d = uigetdir();
 filePattern = fullfile (d, '*.csv');
 files = dir (filePattern);
 unique_months_list = {};
for i = 1:numel(files)
    file_month = files(i).name(07:12); % yyyymm part
    basefilename = files(i).name;
    fullfilename = fullfile (d, basefilename);
    if ismember(file_month, unique_months_list);
        TablesSep {i} = readtable (fullfilename);
        fprintf ('read file %s\n', fullfilename);
    else unique_months_list {end+1} =file_month;
        fprintf ('read file %s\n', fullfilename);
        TablesOct {i} = readtable (fullfilename);
  %  elseif
       % unique_months_list {end+2} =file_month;
       % disp('new'); % do your thing if another month
        %TablesNov {i} = readtable (fullfilename);
    end
end

Edited according to your comment:根据您的评论编辑:

files = dir('d_log_*');
unique_months_list = {};

for i = 1:numel(files)
    file_month = files(i).name(7:12); % yyyymm part
    
    if ismember(file_month, unique_months_list)
        disp('same'); % do your thing if same month
    else
        unique_months_list {end+1} = file_month;
        disp('new'); % do your thing if another month
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM