How do you plot Matlab serial dates on a figure?
4 views (last 30 days)
Show older comments
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.
1 Comment
Answers (1)
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
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!