将时间戳统一为日期字符串

时间:2017-02-14 13:58:02

标签: matlab datetime matlab-table

MATLAB R2015b

我有一个表,其中包含日期字符串和各种格式的时间字符串,每行有两列:

11.01.2016 | 00:00:00 | data

10/19/16 | 05:29:00 | data

12.02.16 | 06:40 | data

我想将这两列用一种通用格式转换为一列:

31.12.2017 14:00:00

我当前的解决方案在每一行上使用循环并将列组合为字符串,检查各种格式以使用适当格式字符串的datetime,然后使用带有所需格式字符串的datestr。日期时间无法自动确定输入字符串的格式。

可以想象,对于大型桌子(大约50000行)来说,速度非常慢。

有没有更快的解决方案?

提前致谢。

2 个答案:

答案 0 :(得分:0)

我试着对代码进行矢量化。诀窍是

  1. 转换表格&gt; <细胞> char-array,然后
  2. 操纵char字符串,然后
  3. 从char-array转换回来&gt; <细胞>表
  4. 此外,有一个重要的方面是以矢量化的方式填充具有'null'字符的具有较短lenth的所有单元格。没有这个,就不可能从单元转换> CHAR-阵列。这是代码。 CLC     清除所有

    %% create Table T
    d={'11.01.2016';
       '10/19/16';
       '12.02.16'};
    
    t={'00:00:00';
      '05:29:00';
      '06:40'};
    dat=[123;
        456;
        789];
    
    T = table(d,t,dat);
    
    %% deal with dates in Table T
    % separate date column and convert to cell
    dd = table2cell(T(:,1));
    % equalize the lengths of all elements of cell
    % by padding 'null' in end of shorter dates
    nmax=max(cellfun(@numel,dd));
    func = @(x) [x,zeros(1,nmax-numel(x))];
    temp1 = cellfun(func,dd,'UniformOutput',false);
    % convert to array for vectorized manipulation of char strings
    ddd=cell2mat(temp1);
    % replace the separators in 3rd and 6th location with '.' (period)
    ddd(:,[3 6]) = repmat(['.' '.'], length(dd),1);
    % find indexes of shorter dates 
    short_year_idx = find(uint16(ddd(:,nmax)) == 0);
    % find the year value for those short_year cases
    yy = ddd(short_year_idx,[7 8]);
    % replace null chars with '20XX' string in desirted place
    ddd(short_year_idx,7:nmax) = ...
        [repmat('20',size(short_year_idx,1),1) yy];
    % convert char array back to cell and replace in table
    dddd = mat2cell(ddd,ones(1,size(d,1)),nmax);
    T(:,1) = table(dddd);
    
    %% deal with times in Table T
    % separate time column and convert to cell
    tt = table2cell(T(:,2));
    % equalize the lengths of all elements of cell
    % by padding 'null' in end of shorter times
    nmax=max(cellfun(@numel,tt));
    func = @(x) [x,zeros(1,nmax-numel(x))];
    temp1 = cellfun(func,tt,'UniformOutput',false);
    % convert to array for vectorized manipulation of char strings
    ttt=cell2mat(temp1);
    % find indexes of shorter times (assuming only ':00' in end is missing
    short_time_idx = find(uint16(ttt(:,nmax)) == 0);% dirty hack, as null=0 in ascii
    % replace null chars with ':00' string
    ttt(short_time_idx,[6 7 8]) = repmat(':00',size(short_time_idx,1),1);
    % convert char array back to cell and replace in table
    tttt = mat2cell(ttt,ones(1,size(t,1)),nmax);
    T(:,2) = table(tttt);
    

答案 1 :(得分:0)

如果你调用两列单元格数组Payrollprotected Vector<GameCell> findPath(int nRow, int nCol) { aBoard[nRow][nCol].setVisited(true); if(aBoard[nRow][nCol].getVal() == 'E') { Vector<GameCell> list = new Vector<GameCell>(); list.add(aBoard[nRow][nCol]); return list; } if(canGoLeft(nRow, nCol)) { if(!aBoard[nRow][nCol - 1].isVisited()) { return findPath(nRow, --nCol); } } if(canGoRight(nRow, nCol)) { if(!aBoard[nRow][nCol+1].isVisited()) { return findPath(nRow, ++nCol); } } if(canGoUp(nRow, nCol)) { if(!aBoard[nRow - 1][nCol].isVisited()) { return findPath(--nRow, nCol); } } if(canGoDown(nRow, nCol)) { if(!aBoard[nRow + 1][nCol].isVisited()) { return findPath(++nRow, nCol); } } System.out.println("You hit a dead end."); return null; } ,那么这样的东西应该有用:

c1

然后你需要删除旧列并将其中的c2放在它们的位置。在内部,c = detestr(datenum(strcat(c1,{' '},c2)), 'dd.mm.yyyy HH:MM:SS') 必须做类似于你正在做的事情,但是,我不确定这是否会更快。我怀疑这是因为(我们可以希望)标准功能得到优化。

如果您的表没有将那些表示为单元格数组,那么您可能需要执行预处理步骤以形成c的单元格数组。

相关问题