Insert missing date and corresponding values in a matrix?

조회 수: 1 (최근 30일)
Alex
Alex 2019년 8월 12일
편집: Adam Danz 2019년 8월 13일
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