How to set default values for a datetime plot?

6 visualizzazioni (ultimi 30 giorni)
Hakan Dingenc
Hakan Dingenc il 5 Nov 2017
Risposto: Peter Perkins il 16 Nov 2017
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.

Risposte (2)

Steven Lord
Steven Lord il 5 Nov 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 .

Peter Perkins
Peter Perkins il 16 Nov 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".

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by