how to add datetime variable to a bar graph
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kantave Greene
el 20 de Jul. de 2017
Comentada: dpb
el 26 de Jul. de 2017
I have a datetime variable with that is 30x1 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.
0 comentarios
Respuesta aceptada
dpb
el 20 de Jul. de 2017
Editada: dpb
el 21 de Jul. de 2017
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.
0 comentarios
Más respuestas (1)
Peter Perkins
el 21 de Jul. de 2017
Starting from R2016b, plotting supports datetimes much more widely, and natively. So
>> d = datetime(2017,1:10,1);
>> x = rand(1,10);
>> bar(d,x)

Prior to R2016b, you'd have to make the bar chart with something like 1:10 for the horizontal axis locations, and then set the tick labels by calling cellstr on your datetimes (or use datetick).
7 comentarios
dpb
el 26 de Jul. de 2017
OK. So to see if I'm on track...the two incantations of plot in graph2d and datetime say actually now end up having different properties accessible, right?
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!