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

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?
Working on it.
If you do wish to use the dates as the X axis, then which MATLAB release are you using?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2018년 6월 12일
With sufficiently new MATLAB:
T = readtable('test.csv');
plot(T.X, T.Y)

댓글 수: 5

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';
Working on it.
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 :(

댓글을 달려면 로그인하십시오.

OCDER
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에 대해 자세히 알아보기

제품

태그

질문:

2018년 6월 12일

댓글:

2018년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by