MATLAB Answers

Loop for comparing and plotting data

3 views (last 30 days)
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?

  0 Comments

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 Oct 2019
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 Comments

Arthur Romeu
Arthur Romeu on 1 Nov 2019
Thank you so much!!
That was some beautiful code.
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 1 Nov 2019
Most welcome! It is just a pleasure to be of some help.

Sign in to comment.

More Answers (0)

Sign in to answer this question.