Function 'subsindex' is not defined for values of class 'datetime'.

I have a simplest possible csv file but still cannot plot(test.X,test.Y) with the above message. Please advise.

6 Comments

What are you trying to plot? Are you plotting the values in the Y col vs the dates in the X col? How are you loading the csv file into the workspace?
This is the format of the file:
X,Y
1/3/09,1.00
1/5/09,0.00
1/7/09,0.00
1/9/09,14.00
1/11/09,106.00
1/13/09,116.00
1/15/09,136.00
1/17/09,109.00
1/19/09,120.00
1/21/09,115.00
That first column is certainly not simple. As that column does not consist simple number, how are they supposed to be interpreted?
You'll have to read them in as strings, then you can make them into datetime objects if you want. To get the entire csv file as a cell array, load it into the workspace as
[~,~,test] = xlsread('test.csv');
You'll now have a 2x11 cell array with the first col as strings for the dates and the second col as the values next to them. From there, you have to decide what you want to do. Your post implies you wanted to plot the data; are you plotting the values against the dates?
If you do wish to use the dates as the X axis, then which MATLAB release are you using?

Sign in to comment.

Answers (2)

With sufficiently new MATLAB:
T = readtable('test.csv');
plot(T.X, T.Y)

5 Comments

To make sure the year stays as 2009, add this line between reading and plotting:
T.X.Year = T.X.Year + 2000;
We have no reason to expect it to refer to 2009 instead of (say) 1609. So instead do
T.X.Format = 'MM/dd/yy';
It's not because people may confuse it with 1609, as you said, but because the plot as you showed would simply show the year as "9", not "09". Hence, if you want the date with a normal year format with the year at the bottom and not part of each tick mark, you do need to add 2000 to each year.
Ah, I had never noticed the XRuler.SecondaryLabel for datetime plots before. And the appropriate code all appears to be built-in, so I can't poke through it :(

Sign in to comment.

FID = fopen('test.csv');
Data = textscan(FID, '%D%f', 'Delimiter', ',', 'Headerlines', 1);
fclose(FID);
Data{1}.Year = Data{1}.Year + 2000; %Assuming you want 2009, etc.
plot(Data{1}, Data{2})

Categories

Products

Tags

Asked:

on 12 Jun 2018

Commented:

on 13 Jun 2018

Community Treasure Hunt

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

Start Hunting!