How to plot successive rows of a table when value is true
1 回表示 (過去 30 日間)
古いコメントを表示
I have attached an example data set. What I would like to do is plot successive rows together if the time between successive rows is <2 hours. Therefore if the data is > 2 hours, we get a single point (row 1), whereas rows 2-4 would be plotted as a line because there is less than 2 hours between those successive points. I have code that works but using this method, I miss the last row of data:
subplot(5,1,1)
unique_vals=unique(gps_data.cpa3);
[ra,ca]=size(gps_sub)
for ii=1:ra-1
gps_plot(ii,:)=gps_sub(ii,:);
int=gps_sub.dt(ii+1)-gps_sub.dt(ii);
if int>hours(2)
gps_plot=rmmissing(gps_plot);
plot(gps_plot.dt,gps_plot.cpa2,'-o','LineWidth',2,'MarkerSize',1)
hold on
clear gps_plot
elseif int<hours(2)
gps_plot=[gps_plot; gps_sub(ii+1,:)];
hold on
end
end
0 件のコメント
採用された回答
the cyclist
2021 年 7 月 22 日
This line of code
for ii=1:ra-1
is looping over all rows except for the last one, so it never "sees" the last row. Therefore, if it is more than 2 hours away from the prior row, it gets missed entirely.
You need to add logic to handle that case.
Personally, I think it might make more sense to set
gps_plot = gps_sub(1,:)
prior to the loop. Then loop over
for ii=2:ra
and if the gap is greater than 2 hours to the prior value, plot accumulated values and start over. If the gap is less than 2 hours, just accumulate.
In essence, I am saying it might be simpler to look back, than look ahead. I would also recommend commenting your code to explain the logic.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!