datetime millisecond conversion puzzling

6 views (last 30 days)
I have been having a puzzling issue with datetime conversion into milliseconds in R2017a.
startRecApp = datesConverted (recordingMatches(1)) +...
milliseconds(DifferencesInMilliseconds * 0.67);
endRecApp = datesConverted(recordingMatches(2)) -...
milliseconds(DifferencesInMilliseconds * 0.33);
z = (endRecApp - startRecApp);
z.Format = 'hh:mm:ss.SSS';
This snippet takes a datetime array (datesConverted) and checks for the difference (z) between the time the application stopped recording and the application started recording. This effectively returns a duration type of variable, which, in this case is 00:28:52.522.
If this is converted into milliseconds (i.e. milliseconds(z)), it returns
ans =
1732180
This is exactly what I needed, as another file, where the data are held, comes from the software with a 1,000 sampling rate (thus each line in the file (sample) represents one millisecond).
A bit later in the code, I have the following:
trigsStimulusStart = milliseconds (duration...
(datesConverted(stimulusOnsetMatches)...
- startRecApp, 'Format', 'hh:mm:ss.SSS'))
This takes the entire array of datesConverted (which are datetimes converted from a char array) and finds the indices corresponding to a stimulus presentation, attempting to return the milliseconds, thus the appropriate lines in the data file. Unlike the first example that returns an integer, this returns:
trigsStimulusStart =
1.0e+06 *
0.0256
0.0639
0.1079
0.1619
0.1977
0.2528
I do not understand this result at all. I am effectively doing the exact same thing, only in this case I subtract the startRecApp from an array, whereas the first example subtracts startRecApp from another variable (namely, endRecApp). How can I make this return integers, just like the "milliseconds(z)"?
Many thanks
  7 Comments
LM_BU
LM_BU on 28 May 2018
Hi jonas,
I am not sure if you've noticed, I used "fix()" and it returned the integers I was looking for. However, I inspected further what you've highlighted, and if I use round(DifferencesInMilliseconds * 0.67), it will return integers, exactly like what fix() did. As such, I will end up using round(), so many thanks for pointing me to the right direction!
Could you turn this into an answer, so that I can accept it?
jonas
jonas on 28 May 2018
I posted one for future reference, but you might aswell accept Rik's answer.
Glad it was resolved!

Sign in to comment.

Accepted Answer

jonas
jonas on 28 May 2018
Problem: why does milliseconds(X) sometimes return doubles?
Reason: Probably because the input value has decimals, in this case following these lines:
+milliseconds(DifferencesInMilliseconds * 0.67)
-milliseconds(DifferencesInMilliseconds * 0.33)
Fix? round() the output or the input to integers.

More Answers (1)

Rik
Rik on 28 May 2018
This actually may already return integers. If you look closely, you see the 1.0e+06* modifier.
trigsStimulusStart =
1.0e+06 *
0.0256
0.0639
0.1079
0.1619
0.1977
0.2528
This means that the first value is 25600 (or close to it), and the last value is 252800 (or close to it). You can change the output format if you like, but the values you have in your output seem already to be what you need.
  5 Comments
LM_BU
LM_BU on 28 May 2018
I have just tried fix(trigsStimulusStart) and it returns positive integers. Will need to investigate whether this is an appropriate solution and why this is needed here contrary to the variable 'z'.
Rik
Rik on 28 May 2018
The fix function rounds toward 0 (so it's equivalent to ceil for negative values, and floor for positive values). The code I gave you is not rounding, just displaying. I would suggest either round or ceil to round your values to integers. I can't explain why this output contains decimal values and your single case doesn't.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!