0

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 !

5
  • if you are supplying a matrix as an input, you cannot specify a formatIn according to the documentation. Try T1= datenum(c1). The input format should be determined automatically. Commented Jun 15, 2017 at 9:51
  • This is also not working. I am getting following error ' The input to DATEVEC was not an array of character vectors.' Commented Jun 15, 2017 at 9:54
  • And one thing I notice there is double quotation marks in time stamp but in matlab example is supposed to be single quotation marks. So how we can replace from double to single quotation? Commented Jun 15, 2017 at 10:10
  • The single quotation marks show that it is a string, there are no quotation marks within the date sting. If your strings contain double quotation marks, remove them using c1 = strrep(c1, '"', '') where the middle argument is single quotes surrounding a double quote, and the last argument is just two single quotes. Commented Jun 15, 2017 at 10:34
  • Yes I was also thinking the same issue. But how can I remove the double quotation from entire c value because this showing the only one value. I have timestamp of say 500 values. How can I convert entire value into single quotation using above function ? Commented Jun 15, 2017 at 10:37

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for this. But as I said I have input of timestamp with double quotation. In your coding you added single quotation along with double quotation. Which is very simple with these small samples. But I have time stamp of cell array of 800 values. How you can added single quotation into entire values ? This is the problem I m facing right now
I haven't added the single quotation to the string, I have added it to make it a string. The single quotations are not in the string! If you have a cell array, the items in it must have a format. Please copy my example t1 creation into your Matlab, and then view t1 in the Workspace, you will see no single quotations. Have you tried the rest of the method?
your example working very fine. But When I try to do the same with my whole data. Its giving me error. I modified code and also given more explanation on why I am getting this error. Please have a look into question again
So how did you solve it? What was the problem? If my answer helped, then please accept it. Feel free to post your own answer if you think it would help others
Sure, I am making

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.