Why am I getting negative seconds when subtracting clock times?

4 views (last 30 days)
I'm trying to create a function that subtracts the start time from the end time of a trial to calculate the trial duration:
trialstart{n_trial} = clock;
trialend{n_trial} = clock;
trialduration{n_trial} = trialend{n_trial}-trialstart{n_trial};
For many trials the code works fine, with trialduration results like the following:
[0,0,0,0,0,7.3425]
However, I notice that when a trial extends from the end of one clock minute into the start of the next clock minute, the trialduration function returns a positive minutes value and a negative seconds value, like this:
[0,0,0,0,1,-53.4844]
This is, of course, not wrong...but I would much prefer to just see a positive number of seconds each time. Any suggestions would be appreciated. Thanks in advance.

Accepted Answer

jonas
jonas on 4 Oct 2018
Edited: jonas on 4 Oct 2018
You could use:
tic %at start of trial
DurationTrial = toc
Or alternatively
StartTrial = datetime('now')
EndTrial = datetime('now')
DurationTrial = EndTrial - StartTrial
Returns duration format

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!