How to set default values for a datetime plot?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have an array of datetime values and another array where each value represents how many times a specific event occurred on the corresponding datetime value. While plotting the occurrence vs. datetime graph, I would like to plot occurrence = 0 for datetime values that do not appear in my array. How can I do this?
I tried doing
t = datetime([2012 8 4; ...]);
y = [10 ...];
syms a b c;
A = [a b c];
[bool, loc] = ismember(A,t);
new_y = piecewise(bool, y(loc), ~bool, 0);
fplot(new_y, [datetime(2012, 8, 4), datetime(2017, 11, 5)]);
but obviously MATLAB cannot convert datetime to sym.
0 comentarios
Respuestas (2)
Steven Lord
el 5 de Nov. de 2017
It sounds like you don't want a line plot but a histogram. The histogram function can show the histogram of a datetime array in release R2017a or later .
0 comentarios
Peter Perkins
el 16 de Nov. de 2017
"datetime values that do not appear in my array" includes a very large number of possibilities. Presumably you have some regular grid of datetime values that you care about, and zeros were not recorded. Create that list of regular datetimes, use iemember to find the dates you do have data for in the full list, and broadcast the non-zeros out to those elements in an array you've preallocated using zeros.
If using R2016b or later, timetables and retime are tailor-made for this kind of thing: 'FillWithConstant".
0 comentarios
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!