2014-07-10 44 views
2

考慮日期date = { 1000000 x 1 }的單元陣列,使其具有不同格式的日期。閱讀日期向量具有不同的格式:Matlab

date = 27-01-2009 
     28-Mar-2003 
      . 
      . 
      . 
     21-02-2003 06:35:20 
     21-02-2003 06:35:20.42 
      . 
      . 
     and so on 

我如何獲得一個100000x3矩陣A = [ year month day ]date

回答

2

方法1

date = { 
    '27-01-2009' 
    '28-Mar-2003' 
    '21-02-2003 06:35:20' 
    '21-02-2003 06:35:20.42'} 

date_double_arr = datevec(date,'dd-mm-yyyy') 
out = date_double_arr(:,1:3) %// desired output 

輸出 -

out = 
     2009   1   27 
     2003   3   28 
     2003   2   21 
     2003   2   21 

方法2

在日 - 月 - 年與時間之間的不一致的情況下,人們可能會想分出前一組並使用它們來獲得像這樣的最終Nx3陣列 -

t1 = cellfun(@(x) strsplit(x,' '), date,'uni',0) 
t2 = cellfun(@(x) x(1), t1) 
t3 = datevec(t2,'dd-mm-yyyy') 
out = t3(:,1:3) %// desired output 
+0

Hey Divakar。我以前試過。我得到這個錯誤。 使用dtstr2dtvecmx時出錯 將日期字符串轉換爲日期編號失敗。 datevec錯誤(第118行) y = dtstr2dtvecmx(t,icu_dtformat); – enigmae

+0

@Nishanth您正在使用什麼版本的MATLAB? – Divakar

+0

我正在使用MATLAB R2013b。 – enigmae