Mismatching tables with different sizes and changing the values

조회 수: 2 (최근 30일)
uzzi
uzzi 2023년 1월 18일
댓글: Siddharth Bhutiya 2023년 1월 27일
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

채택된 답변

Jayant Gangwar
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
uzzi
uzzi 2023년 1월 18일
It works now. Thank you.
Siddharth Bhutiya
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개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by