How to use time (in year) as your x axis for plots when your data is in quarters in Matlab?

29 views (last 30 days)
jj on 12 Nov 2011
Hello everyone,
I am working on time series data, and have been trying to change my x axis to show number of years or to show not all the quarters. My data is in quarters, starting from 1997, ending in 2010. However, when I try plotting my figures, all quarters are shown, and I have't been able to eliminate any quarters. Showing all quarters on the plot makes it really congested looking, despite using xtick_rotate.
Questions: 1. Is it possible to show say just Q1 1997, Q1 2002, Q1 2007, etc?
2. Is it possible to show say 1997, 1998, 1999, 2000? which means I want to show the year instead of quarters, for my quarterly data?
Many thanks!! :)

Walter Roberson on 12 Nov 2011
datetick('x', 'yyyy QQ')
Walter Roberson on 12 Nov 2011
years = 1997:2010
numyears = length(years);
jan1s = zeros(numyears,1);
quarters = zeros(4,numyears);
for Yearno = 1:numyears
thisyear = years(Yearno);
jan1s(Yearno) = datenum([thisyear 1 1]);
quarters(1,Yearno) = jan1s(Yearno);
quarters(2,Yearno) = datenum([thisyear 4 1]);
quarters(3,Yearno) = datenum([thisyear 7 1]);
quarters(4,Yearno) = datenum([thisyear 10 1]);
end
plot(quarters(:),F01);
set(gca,'XTick', jan1s);
datetick('x', 'yyyy');

karan on 12 Nov 2011
Since you havent given the exact possibility ..here is an example of how you would show am/pm in ur plot for every two hours
two_hour = 2/24;
set(gca,'XTick',[two_hour:two_hour:1]); set(gca,'XTickLabel',{'2AM','4AM','6AM','8AM','10AM','12PM','2PM','4PM','6PM','8PM','10PM','12AM'}); set(gca,'XMinorTick','On');
try your case similarly and it will work...the ideology is the same.. i hope this helps
Walter Roberson on 22 Nov 2011
If all the subplots are based on the same time reference, then you can calculate the locations of all of the ticks ahead of time, and then you only need to
set(gca,'XTick', jan1s);
datetick('x', 'yyyy');
for each subplot