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!