How to set default values for a datetime plot?
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Steven Lord
le 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 .
0 commentaires
Peter Perkins
le 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".
0 commentaires
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!