Mismatching tables with different sizes and changing the values

Hello,
I am new to Matlab. I have tables (t1 and t2) with different sizes. The tables show datetime with the range of milliseconds (hh:mm:ss.SSS). I showed and example at 09:50:46.676 and 09:50:46.673 and there is only a millisecond difference.
I want to write a code to find the exact same values of hh:mm:ss in both tables and the difference of milliseconds(SSS) is less than abs(10). If the milliseconds difference is less than abs(10), I want to change the time of the table t2 entry as same as table t1.
%[~,~,~,hx,mx,msx]=datevec(t1);
%[~,~,~,hy,my,msy]=datevec(t2);
%idx=find((hx==hy) & (mx==my) & (msy-msx<abs(10)))
But I am facing an error at the end displaying that the sizes are different. And I don't know how to write the code to change the entry of table t2 same as table t1. Can someone help me?
t1=readtable('t1.xlsx','NumHeaderLines',10,'PreserveVariableNames',true);
t2=readtable('t2.xlsx','NumHeaderLines',10,'PreserveVariableNames',true);
[~,~,~,hx,mx,msx]=datevec(t1);
Error using datevec
The input to DATEVEC was not an array of character vectors or strings.
[~,~,~,hy,my,msy]=datevec(t2);
indexX = [];
indexY = [];
for i = 1:size(hx)
for j = 1:size(hy)
if((hx(i)==hy(j)) & (mx(i)==mx(j)) & (abs(msy-msx)<10))
indexX = [indexX; i];
indexY = [indexY; j];
end
end
end

 Accepted Answer

Hi,
You can use nested for loops to compare datetime values of first table to datetime values of second table and if they satisfy your condition then you can save the idx in a vector, for example your code can be changed as below
[~,~,~,hx,mx,msx]=datevec(t1);
[~,~,~,hy,my,msy]=datevec(t2);
indexX = [];
indexY = [];
for i = 1:size(hx)
for j = 1:size(hy)
if((hx(i)==hy(j)) & (mx(i)==mx(j)) & (abs(msy-msx)<10))
indexX = [indexX; i];
indexY = [indexY; j];
end
end
end
the indexes will be saved in the vectors.

4 Comments

Hello, thank you for your answer. I tried your code and I got an error as "Arrays have incompatible sizes for this operation". I think it is because the size of the tables aren't the same.
Hi, It might be because you need to compare individual elements of msy and msx, I have written in the above code "abs(msy - msx)" but i guess it should be "abs(msy(j)-msx(i))", There might be some other errors as well, I didn't run the code and it is just to show how you can write your code.
For the code shown here you dont really need to convert datetime into datevec. You can directly take the difference of two datetimes and check if the difference is less than 10 ms.
dt1 = datetime; dt2 = dt1 + milliseconds(5);
abs(dt2-dt1) < milliseconds(10)
ans =
logical
1
Also you might want to checkout withtol. If you convert your tables into timetables, then withtol allows you to check for rowtimes within certain tolerance(+/-10 milliseconds for your case).

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!