I want to compare timestamps from two measurement devices to see that both instrumtents have measured the same minutes, and if they haven't find those locations and manipulate the tables so that they do correspond.

9 次查看(过去 30 天)
The first problem is that the measurements use different types of timestamps, but this was solved by converting the array to vector using datevec. Thereafter the seconds in the time where both put to 01 so that would not affect the comparison. To initially see if they were equal I used:
if M1==M2
disp('Timestamps match')
else
disp('Timestamps dont match')
end
The timestamps didn't match however, so now I don't know how to compare each row of the matrix at a time and find out where the timestamps differ. I didn't know how else to program this (so if someone knows an easier way I'm all ears) I looked at the columns to find out where the problem was:
for i=1:6
M1Col=M1(:,i);
M2Col=M2(:,i);
if i==1 && isequal(M1Col,M2Col)==1
disp('Year OK')
elseif i==1 && isequal(M1Col,M2Col)==0
disp('Year not OK')
elseif i==2 && isequal(M1Col,M2Col)==1
disp('Month OK')
elseif i==2 && isequal(M1Col,M2Col)==0
disp('Month not OK')
elseif i==3 && isequal(M1Col,M2Col)==1
disp('Date OK')
elseif i==3 && isequal(M1Col,M2Col)==0
disp('Date not OK')
elseif i==4 && isequal(M1Col,M2Col)==1
disp('Hour OK')
elseif i==4 && isequal(M1Col,M2Col)==0
disp('Hour not OK')
elseif i==5 && isequal(M1Col,M2Col)==1
disp('Minutes OK')
elseif i==5 && isequal(M1Col,M2Col)==0
disp('Minutes not OK')
elseif i==6 && isequal(M1Col,M2Col)==1
disp('Seconds OK')
elseif i==6 && isequal(M1Col,M2Col)==0
disp('Seconds not OK')
end
end
It feels like there is an easier way though...
My second question is that once the location of the difference is found how to "correct" one of the timestamps so that they do match?
Thanks in advance!

回答(2 个)

Jan
Jan 2018-7-10
编辑:Jan 2018-7-10
Name = {'Years', 'Months', 'Days', 'Hours', Minutes', 'Seconds'};
for k = 1:6
if isequal(M1(:,k), M2(:,k))
fprintf('%s ok\n', Name{k});
else
fprintf('%s not ok\n', Name{k});
end
end
Or shorter:
Name = {'Years', 'Months', 'Days', 'Hours', Minutes', 'Seconds'};
Valid = {'not ok', 'ok};
for k = 1:6
match = isequal(M1(:,k), M2(:,k));
fprintf('%s %s\n', Name{k}, Valid{match + 1});
end
And the 2nd question:
My second question is that once the location of the difference is found
how to "correct" one of the timestamps so that they do match?
What about:
M1 = M2;
  5 个评论
Anna
Anna 2018-7-11
Thank you this was most helpful. However, after changing the program a bit M1 and M2 are string instead of double, which gives an error for
rows = find(any(M1 - M2, 2))
as it says "undefined operator '-' for input arguments of type 'string'." How should I proceed with this new problem? Thanks for being patient with me as programming is quite new for me :)

请先登录,再进行评论。


Peter Perkins
Peter Perkins 2018-8-3
You may find that converting everything to datetims is much easier:
>> d1 = datetime(2018,8,3,0,randi(3,5,1),0)
d1 =
5×1 datetime array
03-Aug-2018 00:01:00
03-Aug-2018 00:01:00
03-Aug-2018 00:03:00
03-Aug-2018 00:03:00
03-Aug-2018 00:01:00
>> d2 = datetime(2018,8,3,0,randi(3,5,1),0)
d2 =
5×1 datetime array
03-Aug-2018 00:02:00
03-Aug-2018 00:03:00
03-Aug-2018 00:01:00
03-Aug-2018 00:03:00
03-Aug-2018 00:02:00
>> d1.Minute == d2.Minute
ans =
5×1 logical array
0
0
0
1
0

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by