How to Compare datetimes with milisecs?

조회 수: 4 (최근 30일)
Tiago Dias
Tiago Dias 2018년 2월 6일
편집: Tiago Dias 2018년 2월 8일
Hi, when i compare the i-value from each table it says they are not the same becauase one has milisecs and the other has not, how to delete the mili secs so when i do
u1200lab(1,1)==u1300processo(1,1)
it gives me a positive logical answer?
Because now it says it is not true

채택된 답변

Steven Lord
Steven Lord 2018년 2월 7일
Let's generate two sample datetime objects and set their Format to show just the times with fractional seconds.
N = datetime('now');
N.Format = 'hh:mm:ss.SSS'
X = N + seconds(10*randn);
X.Format = 'hh:mm:ss.SSS'
What's the difference between the two? Is it less than 5 seconds?
d = abs(X-N);
d.Format = 'mm:ss.SSS'
d < seconds(5)
Or you can convert the difference into a number of seconds and compare with the number 5.
sec = seconds(d)
sec < 5
If you want to round the datetime object you could use dateshift to shift to the 'start' or 'end' of the second or shift to the 'nearest' second.
dateshift(N, 'start', 'second')
dateshift(N, 'start', 'second', 'nearest')
dateshift(N, 'end', 'second')
  댓글 수: 1
Tiago Dias
Tiago Dias 2018년 2월 8일
편집: Tiago Dias 2018년 2월 8일
yeah i have tried it and it works so far, D = 1, i will try on my table data too see if it works also thanks
B = datetime('now')
B.Format = 'hh:mm:ss.SSS'
C = datetime('now')
C.Format = 'hh:mm:ss.SSS'
C=dateshift(C, 'start', 'second')
B=dateshift(B, 'start', 'second')
D = isequal(B,C)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Are Mjaavatten
Are Mjaavatten 2018년 2월 7일
s = 30; % The accuracy you desire (in seconds)
close_enough = abs(u1200lab(1,1)-u1300processo(1,1)) < 1/24/3600*s
  댓글 수: 1
Tiago Dias
Tiago Dias 2018년 2월 8일
doesnt work on tables, and both are tables when i load them

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2018년 2월 7일
This has nothing to do with milliseconds, at least not if what you posted is what you are actually doing.
I don't know what you are seeing, but the two mat files you've attached each contain an 8x1 table, each of whose sole variable is an 8x1 datetime. These tables may be a step along a longer journey, but in general, there's no point in creating a table with one variable.
In any case, what I see is
>> u1200lab(1,1)==u1300processo(1,1)
Undefined operator '==' for input arguments of type 'table'.
and that's because you are wanting to compare the datetime values in the table but in fact you are trying to compare two 1x1 tables and there's no comparison operator for tables because they are containers. What you need to do is compare the datetime values. Here's one way:
>> u1200lab.DataColheita(1) == u1300processo.Data(1)
ans =
logical
1
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 2월 8일
In u1200lab.DataColheita(1) the .DataColheita tells it which column of the table to look in. The (1) is then the row number of that column.
Tiago Dias
Tiago Dias 2018년 2월 8일
ok, so that doesnt help me a lot, since i will have multiple columns. will try the other sugestions then

댓글을 달려면 로그인하십시오.

카테고리

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