pltoting a variable with dates on horizontial axis
Answers (2)

11 Comments


4 Comments
I interpreted "my data is date, that is year , quarter" as meaning .1 for Q1, .2 for Q2, etc. I may be wrong.
But in any case, adding a numeric value to a datetime treats the number as a datenum. You code, I think, wants to treat it as a fractional year, and that's problematic, because how long is one tenth of 2018 and how long is one tenth of 2020? That's why calyears requires integer values as inputs. You've used the years function, which does allow fractions, but it does not account for leap days, and in fact:
Y = years(X) returns an array of durations with each element equal to the number of exact fixed-length (i.e. 365.2425 day) years in the corresponding element of X. Y is the same size as X. X may contain non-integer values.
To create an array of calendar years that account for leap days when used in calendar calculations, use the CALYEARS function.
years is really meant as a convenience when you are dealing with very long time spans -- if you want a fixed-length unit of time to express a long duration, 100 "standard" years is easier to understand than 3.1557e+09 seconds. If you are doing a calculation that requires exact numbers, then "three calendar years" doesn't cut it -- the exact length depends on which three years you are talking about. So years(3) is an exact length of time, while calyears(3) is a sort of lazy evaluation that has no unique meaning until you add it to a date.
If you are dealing with calendar arithmetic calyears (and for the same reason, caldays) is what you want to use.
Thanks guys, I did not realize the fraction refered to quarter.
@Peter: Thanks a lot for the elaborate explanation. Did not know about calyear, which seems great for dealing with those annoying leap years!
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!