How can I datetick the x_axis just with the years or decades, when I have data in months ?

12 Ansichten (letzte 30 Tage)
Hello everyone i'm having a problem, i'm trying to datetick de x_axis but since I have 768 points_data but i just need to datetick every 12 or 120 points (corresponding to years or decades). I've been for looking examples but i can't figured out my problem. does anyone can halp me?
n=rand(1,768); %data corresponding to monthly information for 64 years (from 1950 to 2013)
startDate = datenum('01-01-1950'); endDate = datenum('12-31-2012'); xData = linspace(startDate,endDate,768);
plot(n);
set(gca,'XTick',xData) datetick('x','yy','keepticks')

Antworten (1)

dpb
dpb am 13 Mär. 2014
Instead of 'keepticks' use 'keeplimits' and use the optional format date string to show the date as desired. If it's more ticks than want, then generate a vector of datenums for them at the desired interval and use it to set the 'xtick' property array.
There's example of the formatting and generating a fixed interval in the doc for datetick. Since years are whole numbers in datenum format, it's simple to use colon notation as does the above example--it's 10 year interval iirc.
  2 Kommentare
camilo melo
camilo melo am 14 Mär. 2014
Thanks
I've tried to do what you wrote, but still doesn't work. I can't find the example that you related. could you please give me a route to the example?
dpb
dpb am 14 Mär. 2014
Bearbeitet: dpb am 15 Mär. 2014
Show what you did that didn't work...shouldn't take but a few lines to have the pertinent section. And, what does "doesn't work" mean, precisely--that is, what did you get and how isn't that what you want?
The example I was thinking of is at the bottom of page if you just type
doc datetick
Well, it's short enough, here--
Example (based on the 1990 U.S. census):
t = (1900:10:1990)'; % Time interval
p = [ 75.995 91.972 105.711 123.203 131.669 ...
150.697 179.323 203.212 226.505 249.633]'; % Population
plot(datenum(t,1,1),p) % Convert years to date numbers and plot
datetick('x','yyyy') % Replace x-axis ticks with 4 digit year labels.
ADDENDUM:
I was presuming you could see how to set the ticks where desired by generating year values as shown above even though your actual plot data is more dense...that is, the key is
set(gca,'xtick',dn_array)
where
dn_array is your selection of the years you want the ticks at...
ADDENDUM 2:
dn=datenum(1980,[1:768]',1,0,0,0); % a bunch of monthly datenums
y=rand(size(dn)); % and sample data to go along...
plot(dn,y)
dn_tk=datenum([1980:10:1980+ceil(768/12)]',1,1,0,0,0); % 10-yr intervals
set(gca,'xtick',dn_tk) % set ticks as wanted
datetick('x','yyyy','keeplimits') % and format as dates strings

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by