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?
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!