Insert missing date and corresponding values in a matrix?

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Adam Danz
Adam Danz il 12 Ago 2019
Modificato: Adam Danz il 12 Ago 2019
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 Commenti
Jon
Jon il 13 Ago 2019
@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 il 13 Ago 2019
Modificato: Adam Danz il 13 Ago 2019
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

Accedi per commentare.

Più risposte (1)

Jon
Jon il 12 Ago 2019
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 Commenti
Jon
Jon il 12 Ago 2019
Modificato: Jon il 12 Ago 2019
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 il 12 Ago 2019
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.

Accedi per commentare.

Categorie

Scopri di più su Time Series Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by