MATLAB: Am I getting negative seconds when subtracting clock times

clock

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.

Best Answer

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