필터 지우기
필터 지우기

How to plot with YYYYMM date format on x-axis?

조회 수: 2 (최근 30일)
Frits
Frits 2011년 3월 23일
답변: Tiago Dias 2017년 12월 11일
LS,
I would like to plot some data with dates on the horizontal axis. The dates are in YYYYMM format.
When I plot the data set, there is a small horizontal space between e.g. 200911 and 200912. However, there is a large horizontal space between e.g. 200912 and 201001. It is clear why this happens...
Does someone probably have a solution for this issue? Should i convert the YYYYMM format to a date format which matlab understands? How? Or does an option for equidistantial horizontal spacing in graphs exists?
Your help would be very much appreciated.
Best regards,
Frits

채택된 답변

Jiro Doke
Jiro Doke 2011년 3월 23일
This would convert your dates in YYYYMM format to serial dates which would have the correct intervals between the dates.
x = [200911 200912 201001 201002];
serialDates = datenum(num2str(x(:)), 'yyyymm')
Confirm with this:
datestr(serialDates)
  댓글 수: 1
the cyclist
the cyclist 2011년 3월 23일
If you use this approach, then you can use the "datetick" command (which effectively converts datenum-type numeric dates to calendar dates) to create the labels.

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

추가 답변 (2개)

the cyclist
the cyclist 2011년 3월 23일
Yes, you'll need to convert those integers, which are not regular, into something that just count the months. Something like
year = floor(yyyymm/100);
month = yyyymm - 100*year;
Then count the months from some reference year/month.
  댓글 수: 3
the cyclist
the cyclist 2011년 3월 23일
You could do something like this:
yyyymmVector
set(gca,'XTick',yyyymmVector)
set(gca,'XTickLabel',num2str(yyyymmVector))
But see also Jiro's solution.
the cyclist
the cyclist 2011년 3월 23일
In the above comment, "yyyymmVector" was meant to represent your vector of integer dates.

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


Tiago Dias
Tiago Dias 2017년 12월 11일
Hello, i have a question, i got a table, the 1st column i have my time series, DD-MM-YYYY HH:MM:SS and on the others I got my variables.
I want to plot the time vs the variables
time = data(:,1); variables = data(:,2:end);
when i make plot(time,variables) i get an error
how can i fix it besides changing the data to 1 to 300

카테고리

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