Mismatching tables with different sizes and changing the values
4 次查看(过去 30 天)
显示 更早的评论
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);
[~,~,~,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
采纳的回答
Jayant Gangwar
2023-1-18
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 个评论
Siddharth Bhutiya
2023-1-27
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).
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!