Insert missing date and corresponding values in a matrix?

1 次查看(过去 30 天)
Hi!
I have a matrix: Column 1 (Start time), Column 2 (End time), Column 3 (Values). However, start and end time are not continuos. It is yearly data. How can I plot the values against time? A snapshot of the data:
'03-Jan-2018 16:58:09' '04-Jan-2018 08:08:12' 40.58000000000000
'04-Jan-2018 17:03:22' '05-Jan-2018 08:13:39' 45.22000000000000
'05-Jan-2018 17:06:50' '06-Jan-2018 09:19:06' 34.57000000000000
'06-Jan-2018 11:53:42' '06-Jan-2018 13:37:27' 94.42000000000000
'06-Jan-2018 19:19:20' '07-Jan-2018 13:32:37' 84.92000000000000
Thanks!

采纳的回答

Adam Danz
Adam Danz 2019-8-12
编辑:Adam Danz 2019-8-12
If you're looking for timelines that contain segments of start/stop times, here's a method.
data = {
'03-Jan-2018 16:58:09' '04-Jan-2018 08:08:12' 40.58000000000000
'04-Jan-2018 17:03:22' '05-Jan-2018 08:13:39' 45.22000000000000
'05-Jan-2018 17:06:50' '06-Jan-2018 09:19:06' 34.57000000000000
'06-Jan-2018 11:53:42' '06-Jan-2018 13:37:27' 94.42000000000000
'06-Jan-2018 19:19:20' '07-Jan-2018 13:32:37' 84.92000000000000};
startDate = datetime(data(:,1));
stopDate = datetime(data(:,2));
values = [data{:,3}].';
clf()
plot([startDate,stopDate].',[values,values].', 'k-o')
If you'd rather have the line segments connected,
dates = [datetime(data(:,1)), datetime(data(:,2))].';
values = [data{:,3}].';
clf()
plot(dates(:),repelem(values,2,1), 'k-o')
  3 个评论
Jon
Jon 2019-8-13
@Adam I was wondering whether there was a reason why you used
repelem(values,2,1)
rather than the maybe more obvious
[values;values]
or the slightly more familiar
repmat(values,2,1)
I had never seen this way of repeating a row. I guess it's probably just a style choice, but I wondered whether there was anything deeper.
Adam Danz
Adam Danz 2019-8-13
编辑:Adam Danz 2019-8-13
Hi Jon, repelem() and repmat() do not produce the same result.
See how these commands differ in this demo below.
values = [1;2;3;4];
repelem(values,2,1)
% ans =
% 1
% 1
% 2
% 2
% 3
% 3
% 4
% 4
versus
values = [1;2;3;4];
repmat(values,2,1) % Or [values;values]; same thing
% ans =
% 1
% 2
% 3
% 4
% 1
% 2
% 3
% 4

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2019-8-12
I'm not completely clear on what it is you are trying to do, but I think you want to plot the values in the third column against the times in the first column. I assume since these are mixed data types (characters in the first two columns, doubles in the last) that this "matrix" is in fact a cell array. If so, you could do something like
% cell array holding data
data = {
'03-Jan-2018 16:58:09' '04-Jan-2018 08:08:12' 40.58000000000000
'04-Jan-2018 17:03:22' '05-Jan-2018 08:13:39' 45.22000000000000
'05-Jan-2018 17:06:50' '06-Jan-2018 09:19:06' 34.57000000000000
'06-Jan-2018 11:53:42' '06-Jan-2018 13:37:27' 94.42000000000000
'06-Jan-2018 19:19:20' '07-Jan-2018 13:32:37' 84.92000000000000}
% get MATLAB datetime values from cell array of character vectors
time = datetime(data(:,1));
% get measured values from last column as a vector of doubles
measurements = cell2mat(data(:,3))
plot(time,measurements)
  10 个评论
Jon
Jon 2019-8-12
编辑:Jon 2019-8-12
As @Adam Danz points out, you can easily vectorize the above code and eliminate the loop, which is always a good idea. I just wanted to make sure you were clear about what was getting plotted and so made it a little more explicit with the loop. If you are doing this for big data sets it would definitely be better to vectorize it. In any case the vectorized approach is definitely much cleaner, so you should just use that.
Alex
Alex 2019-8-12
Thanks Jon for the explanation. It really helped me to understand. In fact, the answer from Adam worked for my case, and it was exactly what I was looking for.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by