Unexpected issue with datetime

11 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2021
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.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!