Fix for " The input to DATENUM was not an array of strings."
5 views (last 30 days)
Show older comments
I am attempting to use datenum on my table (one column, first column) of dates to convert it into a separate variable of just a series of numbers
dataset = readtable('equity_dataset_30.csv');
data = dataset(:,2:31);
date = dataset(:,1);
date_num = datenum(date, 'yyyy-mm-dd');
This last line keeps returning the error: " The input to DATENUM was not an array of strings."
Could someone show what I'm missing here?
0 Comments
Accepted Answer
Steven Lord
on 7 Oct 2016
The variable dataset is a table. When you index into a table with parentheses, the result is also a table. When you index into a table using curly braces, the result is the type of the underlying data. Compare:
x = (1:5).';
y = int8(x.^2);
T = table(x, y);
tableSubarray = T(1:3, 'x');
doubleSubarray = T{1:3, 'x'};
int8Subarray = T{4:5, 'y'};
whos tableSubarray doubleSubarray int8Subarray
You don't want to pass something like tableSubarray into datenum -- you want to pass something like doubleSubarray or int8Subarray (only you want to specify a char array, not a numeric array.)
Once you have the subarray of your table containing character representations of your dates, I recommend that you convert them into a datetime array rather than converting them into serial date numbers using datenum.
2 Comments
Peter Perkins
on 10 Oct 2016
Steve's right, but perhaps even simpler, if you want to access a single variable, would be
doubleSubarray = T.x(1:3);
int8Subarray = T.y(4:5);
More Answers (1)
Massimo Zanetti
on 7 Oct 2016
Most probably your variable "date" is not an array of strings.
7 Comments
NN
on 26 Oct 2020
thank you .It worked.
But i am getting spike like output when i plot datenum and forecasted output.Doesnt look lik wave form .
Kindly advice
Walter Roberson
on 26 Oct 2020
I do not see any obvious problem with the plot. It looks to me like what you would expect for a plot displayed with an unfortunate aspect ratio. Try zooming it.
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!