Function 'subsindex' is not defined for values of class 'datetime'.
Mostrar comentarios más antiguos
I have a simplest possible csv file but still cannot plot(test.X,test.Y) with the above message. Please advise.
6 comentarios
Matt Dickson
el 12 de Jun. de 2018
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?
Matt Dickson
el 12 de Jun. de 2018
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?
alpedhuez
el 12 de Jun. de 2018
Walter Roberson
el 12 de Jun. de 2018
If you do wish to use the dates as the X axis, then which MATLAB release are you using?
Stephen23
el 13 de Jun. de 2018
See also earlier question and discussion:
Respuestas (2)
Walter Roberson
el 12 de Jun. de 2018
With sufficiently new MATLAB:
T = readtable('test.csv');
plot(T.X, T.Y)
5 comentarios
Matt Dickson
el 12 de Jun. de 2018
To make sure the year stays as 2009, add this line between reading and plotting:
T.X.Year = T.X.Year + 2000;
Walter Roberson
el 12 de Jun. de 2018
We have no reason to expect it to refer to 2009 instead of (say) 1609. So instead do
T.X.Format = 'MM/dd/yy';
alpedhuez
el 12 de Jun. de 2018
Matt Dickson
el 13 de Jun. de 2018
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.
Walter Roberson
el 13 de Jun. de 2018
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 :(
OCDER
el 12 de Jun. de 2018
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})
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!