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
Matt Dickson
2018년 6월 12일
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
2018년 6월 12일
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
2018년 6월 12일
Walter Roberson
2018년 6월 12일
If you do wish to use the dates as the X axis, then which MATLAB release are you using?
Stephen23
2018년 6월 13일
See also earlier question and discussion:
답변 (2개)
Walter Roberson
2018년 6월 12일
With sufficiently new MATLAB:
T = readtable('test.csv');
plot(T.X, T.Y)
댓글 수: 5
Matt Dickson
2018년 6월 12일
To make sure the year stays as 2009, add this line between reading and plotting:
T.X.Year = T.X.Year + 2000;
Walter Roberson
2018년 6월 12일
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
2018년 6월 12일
Matt Dickson
2018년 6월 13일
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
2018년 6월 13일
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
2018년 6월 12일
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})
카테고리
도움말 센터 및 File Exchange에서 Time Series Events에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!