datetime millisecond conversion puzzling
조회 수: 3 (최근 30일)
이전 댓글 표시
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
jonas
2018년 5월 28일
I posted one for future reference, but you might aswell accept Rik's answer.
Glad it was resolved!
채택된 답변
jonas
2018년 5월 28일
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.
댓글 수: 0
추가 답변 (1개)
Rik
2018년 5월 28일
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
Rik
2018년 5월 28일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!