Unexpected issue with datetime

조회 수: 5 (최근 30일)
Riccardo Tronconi
Riccardo Tronconi 2021년 6월 10일
댓글: Riccardo Tronconi 2021년 6월 10일
Hi guys! I have this error Right hand side of an assignment must be a datetime array or text representing dates and time related to:
report12{c,3} = duration;
where
duration = end_time - start_time;
At first these entities where initialized to:
report12.B3 = NaT(height(B),1,'Format', 'HH:mm:ss.SSSSSSSSS' );
duration= NaT(height(B),1,'Format', 'HH:mm:ss.SSSSSSSSS' );
start_time = NaT;
end_time = NaT;
At the same time this works:
report12{c,1} = start_time;
Of course, these values are not null when I subtract and assign them.

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 10일
duration= NaT(height(B),1,'Format', 'HH:mm:ss.SSSSSSSSS' );
No matter what you name the variable, NaT creates a datetime object.
duration = end_time - start_time;
That would replace the variable named duration with a duration object .
Right hand side of an assignment must be a datetime array or text representing dates and time related to:
report12{c,3} = duration;
You failed to tell us that report12 is a table() object and that you initialized the third column of it as a datetime object. Then report12{c,3} as the destination of the assignment would be trying to store the value in the variable named duration with the duration object that is created by subtracting two datetime objects. But datetime objects can only have datetime objects stored in them.
  1. Do not use a variable named duration if you can avoid it; it confuses people compared to the dataclass duration
  2. Do not store duration objects inside datetime objects
  3. The duration() equivalent of NaT is duration(nan,nan,nan) or any duration(nan,0,0) or any combination of 3 values in which at least one of them is nan.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by