Unexpected issue with datetime

9 次查看(过去 30 天)
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