简体   繁体   English

从文件夹中的多个csv文件读取时间序列:MATLAB

[英]Reading timeseries from multiple csv files in a folder: MATLAB

I have a bunch of csv files in a folder in following format, I want to extract complete time series from each (the numeric part from line #17), identify duplicate record and merge them in a ascending order according to year and date. 我在以下格式的文件夹中有一堆csv文件,我想从每个文件中提取完整的时间序列(第17行的数字部分),识别重复的记录,然后根据年份和日期以升序合并它们。 Specific csv file is accessible via google drive link below 可通过下面的Google Drive链接访问特定的csv文件

    wnsnum  1           
    paroms  Waterhoogte                                                             
    loccod  HOEKVHLD                                                                
    locoms  Hoek van Holland                                                        
    rks_begdat  1993 07 09          
    rks_begtyd  00:00           
    rks_enddat  2014 31 12          
    rks_endtyd  23:50           
    begdat  begtyd  enddat  endtyd  rkssta
    1993 07 09  00:00   2007 31 12  23:50   D     
    2008 01 01  00:00   2009 30 12  23:50   G     
    2009 31 12  00:00   2009 31 12  23:50   O     
    2010 01 01  00:00   2011 17 06  18:40   G     
    2011 17 06  18:50   2011 18 06  18:50   O     
    2011 18 06  19:00   2014 31 12  23:50   G     
    datum   tijd    bpgcod  waarde  kwlcod
    1993 07 09  00:00       -70 0
    1993 07 09  00:10       -69 0
    1993 07 09  00:20       -68 0
    1993 07 09  00:30       -67 0
    1993 07 09  00:40       -68 0
    1993 07 09  00:50       -70 0
    1993 07 09  01:00       -69 0
    1993 07 09  01:10       -69 0
    1993 07 09  01:20       -68 0
    1993 07 09  01:30       -67 0
    1993 07 09  01:40       -65 0
    1993 07 09  01:50       -64 0
    1993 07 09  02:00       -62 0
    1993 07 09  02:10       -61 0
    1993 07 09  02:20       -61 0
    1993 07 09  02:30       -59 0
    1993 07 09  02:40       -58 0
    1993 07 09  02:50       -55 0

Now my code is working in a following way: 现在,我的代码以以下方式工作:

SL_files = dir(sprintf('%s%s%s',fullfile(dirName),'\','*.csv'));
for idx = 1:size(SL_files,1)
    disp(SL_files(idx,1).name)
    fid = fopen(sprintf('%s%s%s',fullfile(dirName),'\',SL_files(idx,1).name)); 
    data = textscan(fid, '%s %f %f %f %f %f %f', ...
      'Delimiter',',', 'MultipleDelimsAsOne',1,'headerlines',16);

    fclose(fid);
end

Now I could read the file. 现在我可以读取文件了。 Now my problem is how to combine multiple files' data into one matrix and arrange them in a ascending order according to year and day values. 现在我的问题是如何将多个文件的数据合并到一个矩阵中,然后根据年和日的值将它们按升序排列。 Thanks! 谢谢!

I finally solve my problem. 我终于解决了我的问题。 Here is the code: 这是代码:

numMat_All = [];
for idx = 1:size(SL_files,1)
    disp(SL_files(idx,1).name)
    fid = fopen(sprintf('%s%s%s',fullfile(dirName),'\',SL_files(idx,1).name)); 
    data = textscan(fid, '%s %f %f %f %f %f %f', ...
      'Delimiter',',', 'MultipleDelimsAsOne',1,'headerlines',16);
    fclose(fid);

    CharCell = data{1,1};
    result = regexprep(CharCell,'[\s;:]+',' ');
    numMat = cell2mat(cellfun(@str2num, result(:,1:end), 'UniformOutput', false));

    numMat_All = [numMat_All;numMat];
    data = []; CharCell = []; result = []; numMat = []; 
end

dt = datetime([numMat_All(:,1:5), repmat(0,length(numMat_All),1)]);
T = table(dt,numMat_All(:,[6:7]));    
T1 = sortrows(T,'dt');

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

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