I write this code for calculating datenum values:
Test_table = table2dataset(Test_table);
t1 = Test_table (:,3);
c1 =dataset2cell(t1);
C1 = strrep(c1(2:end), '"', '');
formatIn = 'yyyy-mm-dd HH:MM:SS.FFF';
T1= datenum(C1(1:end),formatIn);
I have data in table format, which are converted into datasets, then from there I extracted timestamp in the form of a cell array. But when I run the code I am getting the following error:
The input to DATENUM was not an array of character vectors.
The entire timestamps (t1
) in cell array format are uploaded into this site here: But still getting an error , 'Error using strrep
Cell elements must be character vectors.' . What is wrong here ?
Solution :
After the struggling of 1 day, I am able to solve this problem. Actually the error I am getting because I have data sets in following format [1x1 string]
which is wrong hence I am geeting error for my code. So to solve this problem I used cellstr
function which converted my entire datasets into cell. And hence now working. So correct code should be like this ,
t1 = table2dataset(:,3);
C1 = cellstr(t1);
d1 = strrep(C1, '"', '');
formatIn = 'yyyy-mm-dd HH:MM:SS.FFF';
t1 = datenum(d1, formatIn);
Hope this help for future visitor !
答案 0 :(得分:1)
好的,你有以下单元格数组:
t1 = {'"2009-04-13 04:20:00.000"'
'"2009-04-13 04:30:00.000"'
'"2009-04-13 04:40:00.000"'
'"2009-04-13 04:50:00.000"'
'"2009-04-13 05:00:00.000"'
'"2009-04-13 05:10:00.000"'
'"2009-04-13 09:40:00.000"'
'"2009-04-13 09:50:00.000"'
'"2009-04-13 10:00:00.000"'
'"2009-04-13 10:10:00.000"'}
每个日期都是一个字符串,因为它被单个引号'
包围,并且由于您的数据提取,每个日期包含双引号。要从整个单元格数组中删除双引号,请将其替换为空字符串''
(这是两个单引号,两者之间没有空格)。使用strrep
执行此操作。
c1 = strrep(t1, '"', '');
% c1 = {'2009-04-13 04:20:00.000'
% '2009-04-13 04:30:00.000'
% '2009-04-13 04:40:00.000'
% '2009-04-13 04:50:00.000'
% '2009-04-13 05:00:00.000'
% '2009-04-13 05:10:00.000'
% '2009-04-13 09:40:00.000'
% '2009-04-13 09:50:00.000'
% '2009-04-13 10:00:00.000'
% '2009-04-13 10:10:00.000'}
然后您可以将其传递给datevec
或datenum
。
formatIn = 'yyyy-mm-dd HH:MM:SS.FFF';
T1 = datevec(c1, formatIn);
% year month day hour minutes seconds
%T1=[2009 4 13 4 20 0
% 2009 4 13 4 30 0
% 2009 4 13 4 40 0
% 2009 4 13 4 50 0
% 2009 4 13 5 0 0
% 2009 4 13 5 10 0
% 2009 4 13 9 40 0
% 2009 4 13 9 50 0
% 2009 4 13 10 0 0
% 2009 4 13 10 10 0]