Loop for comparing and plotting data

1 回表示 (過去 30 日間)
Arthur Romeu
Arthur Romeu 2019 年 10 月 30 日
Hello,
I currently have this code
for k = 1:size(DayGroups,1)
for j = 1:size(DayGroups_t,1)
if datetime(DayGroups_t{j,1}{1,1}) == datetime(DayGroups{k,1}{1,1})
figure(k)
plot(DayGroups{k}{:,2}, DayGroups{k}{:,3:end}, 'bp', DayGroups_t {j}{:,2}, DayGroups_t{j}{:,3:end}, 'r:')
grid
end
end
end
I'm attaching DayGroups.mat and DayGroups_t.mat so you can take a look. they contain groups of tables with datetimes, X coordinates and Y coordinates.
What I am trying to do is to plot a chart only when both datetimes are the same. In order to do that, I'm trying to create a loop where every group from DayGroups_t is compared with the first group from DayGroups, then the second, and so on.
If the first group from DayGroups_t that has the same datetime as the first group from DayGroups, then a chart is plotted containing the X and Y data from those groups.
If not, j becomes j+1 (until it reaches the size of DayGroups_t) and the comparison happens between the second group of DayGroups_t and the first group of DayGroups.
When all groups from DayGroups_t get compared with the first group from DayGroups, then k becomes k+1 and everything happens again, but with the second group from DayGroups, until all the loop has been completed and I end up with the beautiful images of both things that happened on the same day.
However, this code is not working at all. And I have no idea why! Can someone please help me?

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 10 月 30 日
Hi
Here is the corrected solution:
load DayGroups.mat
load DayGroups_t.mat
for k = 1:size(DayGroups,1)
for j = 1:size(DayGroups_t,1)
if strcmp(string(datetime(DayGroups_t{j,1}{1,1})), string(datetime(DayGroups{k,1}{1,1})))
figure(k)
plot(DayGroups{k}{:,2}, DayGroups{k}{:,3:end}, 'bp', DayGroups_t {j}{:,2}, DayGroups_t{j}{:,3:end}, 'r:')
grid
end
end
end
Good luck
  2 件のコメント
Arthur Romeu
Arthur Romeu 2019 年 11 月 1 日
Thank you so much!!
That was some beautiful code.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 11 月 1 日
Most welcome! It is just a pleasure to be of some help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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