Fix for " The input to DATENUM was not an array of strings."

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?

 Accepted Answer

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

Thank you very much this solved my question!
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);

Sign in to comment.

More Answers (1)

Most probably your variable "date" is not an array of strings.

7 Comments

Thanks I thought this might be the problem so I tried converting my date column into an array of strings using
num2str(date)
but it returns the error message: "You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts."
-My date column is currently formatted like this...
'2005-01-03'
'2005-01-04'
'2005-01-05' ...and so on.
How should I go about converting my date column into an array of strings so I can use 'datenum' on it?
kindly see the below code .i also get the same error.kindly advice
%reading date
Data=readtable('2019_SysLoad.xlsx');
save 2019_SysLoad.mat Data
testdates=Data((5834:end),1)};
testdates1=datenum(testdates1,'mm-dd-yyyy');
thank you .But it gives the following error
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
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
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.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!