I am not certain what you want, since the tables are difficult to interpret.
Try this to see if it provides the necessary result:
c = 1
for k = 1:size(TR_SoftStart,1)
Years = abs(year(PC_SoftStart) - year(TR_SoftStart.Data(k))) < 1;
Months = abs(month(PC_SoftStart) - month(TR_SoftStart.Data(k))) < 1;
Days = abs(day(PC_SoftStart) - day(TR_SoftStart.Data(k))) < 1;
Hours = abs(hour(PC_SoftStart) - hour(TR_SoftStart.Data(k))) < 1;
Minutes = abs(minute(PC_SoftStart) - minute(TR_SoftStart.Data(k))) <= 1;
Test = Years & Months & Days & Hours & Minutes;
if any(Test,'all')
disp(k)
else
errors_SoftStart(c,:) = TR_SoftStart(k,:);
c = c + 1;
end
end
end
I did my best to document how the code works in the comments. Briefly, rather than doing a direct equality comparison, it subtracts the relevant parts of the datetime variables, and then the minute values of the datetime values in the two table arrays from each other, takes the absolute value of the difference (implicitly accounting for the ±1 minute), and tests to see if it is less than or equal to 1. (The results of the subtraction are integers, so floating-point approximation error is not a concern.) It then uses the any function to see if there are any values in the ‘Test’ logical matrix, and proceeds with the rest of your code. I eliminated the find call, since it is redundant here (you are not returning any indices). I checked the code by printing (here for the minute values, also for the others):
Check1 = minute(PC_SoftStart(1:4,:)) - minute(TR_SoftStart.Data(k))
Check2 = abs(minute(PC_SoftStart(1:4,:)) - minute(TR_SoftStart.Data(k)))
Check3 = Test(1:4,:)
View = [any(Test,'all') sum(Test,'all')]
to the Command Window to be certain the code does what I want it to for the first two iterations of the loop, and it does. (I did not include those assignments in the code I posted here, since they are diagnostic only. I include them in the event you want to check that yourself.)
I did not get good results from subtracting the complete datetime values, so I decided to do the subtraction on each part of the datetime values, then logically combine them (&) to get the result. It might be possible to do this with datenum arrays, however they introduce other problems, so I decided to do this with datetime arrays instead.
I thought about I would approach this for most of the day, and finally came up with this code, that appears to work. It is an interesting problem, and I learned a bit in the process of creating it!