How do you plot Matlab serial dates on a figure?

4 views (last 30 days)
I am trying to plot dates on a figure on the x-axis, and show actual dates versus Matlab serial date numbers (i.e. I want to plot 8/31/2011 vs. 735112 on the figure). How do you do that?
Here are my commands:
importfile('chk.xls');
cDate = Date;
cDate = x2mdate(cDate, 0);
str = datestr(cDate, 1);
cDt = cellstr( str )
I converted cDate to a cell array thinking it would have done the trick, but it didn't. I am now stuck with a cell vector of the same size as my variables from chk.xls, which are double vectors as the same size.

Answers (1)

per isakson
per isakson on 2 Sep 2012
Edited: per isakson on 3 Sep 2012
Did you read and understand the documentation on datetick?
You need to do something like
plot( cDate, data )
datetick( 'x', 'mm/dd/yyyy' )
where cDate is a Matlab serial date number
Did I completely misunderstand what you try to achieve? Maybe I did. Does this do it?
plot( cDate, cDate, 'd' )
datetick( 'y', 'mm/dd/yyyy', 'keeplimits' )
.
-- In response to comment 1 ---
By mistake, I wrote 'keeplimits' above instead of 'keepticks'. However, that revealed you did not read the documentation carefully enough. 'keepticks' makes sure that the ticks, which where assigned by plot, are kept. The problem is that the "datestrings" may be very wide and to compensate for that datatick might choose fewer ticks.
Default values don't always give a good result.
Low level stuff: The handle graphic object, axes, has properties, which controls many aspects of the diagram. With the property, XTick, you can control the positions and number of ticks and 'keepticks' makes sure that datetick doesn't tamper with your choice.
  3 Comments
harsh
harsh on 2 Sep 2012
Sweet! I used
plot(cDate, DeltaP);
datetick( 'x', 'mm/dd/yyyy');
I tried 'keeplimits' but it only gave me one date on the x-axis, without keep limit, it plots the whole graph, but the x-axis stretches from 1/1/2011 to 1/1/2013. How do I make the dates on the figures be from 8/31/2011 to 8/31/2012?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!