MATLAB: How to add datetime variable to a bar graph

bardatetime

I have a datetime variable with that is 30×1 representing 30 days. The original datetime variable has yyyy-MM-dd HH-mm-ss. How can I create a bar graph with the x-axis having the datetime values? I can do it with plot but not bar.

Best Answer

For some reason TMW hasn't yet gotten around to making the rest of the plotting routines datetime aware so you've got to revert to the venerable datenum(*):
Try
dt=datenum(2017,1,1:4).'; % a short time series vector
y=randn(4,30); % some y-data to go along with it
hBar=bar(dt,y); % bar graph vs the time
datetick('x','keeplimits','keepticks') % set the axis to display time strings
See
doc datetick % and friends
for the details on formatting, etc., ...
ADDENDUM
() *NB: Even with plot using a datetime- object, internal to axes the date values on the axis are datenum doubles, not datetime objects. TMW pretty-much had to do this for compatibility purposes. Hence, while there's a "pretty" user interface and the new class/object has some useful properties, the old-style datenum isn't going to go away any time soon.