How to set default values for a datetime plot?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Steven Lord
2017-11-5
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 个评论
Peter Perkins
2017-11-16
"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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!