How to create a time vector that is incremented between two datetime strings?
조회 수: 17 (최근 30일)
이전 댓글 표시
I am trying to plot local time as a function of pressure recordings but I have to manually create the time vector since my data aqcuisition system does not record the time each sample is recorded. I only have the start time so I calculated the duration of the data acquisition and then tried to increments the start time to the end time using the time sampling rate (dt). But my t outputs only a single time rather than an vector. How can I resolve this? My final plot should have an x-axis that is in HH:mm:ss.SSS format.
startTime = datetime(2023,01,10,11,22,01.700);
dt = 0.001;
duration = dt*length(data); % seconds
endTime = datetime(2023,01,10,11,22,01.700+duration);
t = [startTime:dt:endTime];
댓글 수: 0
채택된 답변
Star Strider
2023년 1월 11일
편집: Star Strider
2023년 1월 11일
It would help to have some idea of what you want to do, and what ‘data’ is (and for good measure ‘data’ itself).
Try something like this —
data = (0:999).'; % Create Missing Variable
startTime = datetime(2023,01,10,11,22,01.700);
endTime = startTime + days(0.001*(0:size(data,1)-1)).' % Create Column Vector
EDIT — (11 Jan 2023 at 1:42)
I am not certain what you want to do, however there are several examples in the documentation section Generate Sequence of Dates and Time.
.
댓글 수: 0
추가 답변 (2개)
the cyclist
2023년 1월 11일
I think you intended
endTime = datetime(2023,01,10,11,22,01.700)+duration;
rather than
endTime = datetime(2023,01,10,11,22,01.700+duration);
댓글 수: 0
Stephen23
2023년 1월 11일
편집: Stephen23
2023년 1월 11일
"But my t outputs only a single time rather than an vector. How can I resolve this?"
Of course you can use the COLON operator (you do not need to use LINSPACE), but you do need to tell MATLAB what the units are (by default the COLON operator will assume a step size in days, not in seconds, as the documentation explains here). The easiest way to do this is to use DURATION objects to specify those times:
data = rand(1,123);
st = datetime(2023,01,10,11,22,01.700, 'Format','y-MM-dd HH:mm:ss.SSS');
dt = seconds(0.001); % make this a DURATION object.
du = dt*(numel(data)-1); % Do NOT use name DURATION.
et = st+du;
t = st:dt:et;
t(:)
Here is an alternative, simple, numerically robust approach:
t = st+seconds(0.001)*(0:numel(data)-1); % sample times
t(:)
"My final plot should have an x-axis that is in HH:mm:ss.SSS format. "
That might suit a DURATION object better:
tod = timeofday(t);
tod.Format = 'hh:mm:ss.SSS';
plot(tod,data)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!