Insert missing date and corresponding values in a matrix?
Show older comments
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!
Accepted Answer
More Answers (1)
Jon
on 12 Aug 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 Comments
Alex
on 12 Aug 2019
Sorry, but I'm still not understanding. You have two columns that hold time values. Do you want to plot your data against the times in the first column, the times in the second column, or maybe somewhere between the times in the first and the second column?
What have you tried to do already, and what problem did you encounter?
Alex
on 12 Aug 2019
Jon
on 12 Aug 2019
So you want a plot that looks like a square wave with a constant value of 40.58000000000000 from '03-Jan-2018 16:58:09' to '04-Jan-2018 08:08:12', then zero from '04-Jan-2018 08:08:12' to '04-Jan-2018 17:03:22' then 45.22000000000000 from '04-Jan-2018 17:03:22' to '05-Jan-2018 08:13:39' etc?
Jon
on 12 Aug 2019
So the vertical axis are the values from the third column, and the horizontal axis are time intervals? What have you tried already. What problem did you have?
Alex
on 12 Aug 2019
Jon
on 12 Aug 2019
Does this do what you want
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
start = datetime(data(:,1));
finish = datetime(data(:,2));
% get measured values from last column as a vector of doubles
measurements = cell2mat(data(:,3))
% make loop to plot each data segment
figure
for k = 1:length(start)
plot([start(k),finish(k)],[measurements(k) measurements(k)],'Color','k')
hold on
end
hold off
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
on 12 Aug 2019
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
