Reputation: 485
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 !
Upvotes: 0
Views: 246
Reputation: 30101
Okay, so you have the following cell array:
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"'}
Each date is a string, because it is surrounded by single quotation marks '
, and each date contains double quotes because of your data extraction. To remove the double quotes from the entire cell array, replace them with the empty string ''
(this is two single quotes with no space between). Do this using 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'}
Then you can pass this to datevec
or 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]
Upvotes: 1