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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by