필터 지우기
필터 지우기

plotting all the years of a time period vertically to the x-axis

조회 수: 1 (최근 30일)
Dear all,
I have ns=2838 daily observations y. I plot these data using the following commands
startDate = datenum('07-01-1996');
endDate = datenum('07-30-2007');
xData = linspace(startDate,endDate,ns);
figure
plot(xData, y )
datetick( 'x','yyyy','keepticks' )
In this case, I obtain the following sequence of years on the x-axis:
1995, 19997, 1998,2000,2001,2002,2004, 2005, 2006, 2008
But I would like to have, vertically to the x-axis, all the years starting from 01/1996 till 30/2007, that is
01/1996, 01/1997, 01/1998 , 01/1999 01/2000 , 01/2001, 01/2002 ...
How could I modify the above code to do that?
Thanks in advance

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 29일
ns=2838;
startDate = datenum('07-01-1996');
endDate = datenum('07-30-2007');
xData = linspace(startDate,endDate,ns);
plot(xData, randn(ns))
xt=arrayfun(@num2str,(1996:2007),'un',0)
xl=get(gca,'xlim')
set(gca,'xtick',linspace(xl(1) ,xl(2),numel(xt)),'xticklabel',xt)
  댓글 수: 2
antonet
antonet 2013년 8월 29일
thanks Azzi. it works fine. the only remaining problem is that the plot ends before july 2007. how is that possible
Thanks for your patience!
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 29일
close
ns=2838;
startDate = datenum('07-01-1996');
endDate = datenum('07-30-2007');
xData = linspace(startDate,endDate,ns);
plot(xData, randn(ns))
xt=arrayfun(@num2str,(1996:2007),'un',0)
xl=[startDate endDate]
set(gca,'xlim',xl)
set(gca,'xtick',linspace(xl(1) ,xl(2),numel(xt)),'xticklabel',xt)

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 29일
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 29일
Give a short example with data
antonet
antonet 2013년 8월 29일
For instance,
ns=2838;
startDate = datenum('07-01-1996');
endDate = datenum('07-30-2007');
xData = linspace(startDate,endDate,ns);
figure
subplot(2, 1, 1);
plot(xData, randn(ns))
datetick('x','yyyy','keepticks')
Two comments:
A) I do not obtain all the years on the x-axis. the year 1999, for instance, is missing. I want to have all the years from 1996 to july 2007 on the x axis.
B) As you can see, the matlab graph does not start from 1996 but from 1995. Similarly, the graph ends in 2008 which is a redundant year since the time period ends in july 2007. So can "throw" these redundant years?
Many thanks

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by