2017-07-31 91 views
0

我有字符串的Matlab表,我想将它转换为datenum格式。我想:将表字符串转换为datenum

datenum(Tbl) 

,但我收到的错误:

Error using datenum (line 181) 
DATENUM failed. 

Caused by: 
    Error using datevec (line 103) 
    The input to DATEVEC was not an array of character vectors. 

这里是我的TBL的一个样本:

Tbl = table; 
Tbl.('A') = {'29/07/2017'; 0}; 
Tbl.('B') = {'29/07/2017'; '31/07/2017'}; 
+0

添加了Tb例如。 – JohnAndrews

+0

添加了Tbl作为文本。 – JohnAndrews

+1

你是如何在字符串表中获得'0'的? – excaza

回答

1

转换table to an array第一,然后应用datenum非常久远日期的格式。在您的数据的数字纳入很奇怪,但不管怎么说,这是一个解决方案:

numdate= table2array(Tbl);  %Converting table to array 
ind = cellfun(@ischar,numdate); %Finding logical indices of dates stored as char array 
%Finding serial date number of dates; not doing anything on numeric values 
numdate(ind)=cellfun(@(x) datenum(x, 'dd/mm/yyyy'), numdate(ind),'un',0); %Serial Datenums 
%Converting back the date number serials into dates  
dateback=numdate; dateback(ind)=cellfun(@datestr,numdate(ind),'un',0); 

输出:

>> numdate 

numdate =  
    [736905] [736905] 
    [  0] [736907] 

>> dateback 

dateback = 
    '29-Jul-2017' '29-Jul-2017' 
    [   0] '31-Jul-2017' 
+0

谢谢。但是,当我做datetr(...)我得到错误的日期。所以它不起作用。 – JohnAndrews

+0

@JohnAndrews查看编辑 –

2

尝试varfun

varfun(@datenum,Tbl) 

生产

datenum_A datenum_B 
_________ _________ 

12791  12791  
    0  13521  

选项2

或者,可以在这样的时间做这一个柱:

Tbl.('A') = cellfun(@datenum,Tbl.('A')) 

生产

TBL =

A   B  
_____ ____________ 

12791 '29/07/2017' 
    0 '31/07/2017' 

然后你就可以做到这一点'B'等

+0

这些选项是否适合您? – informaton