Loop for comparing and plotting data

1 次查看(过去 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
Most welcome! It is just a pleasure to be of some help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by