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];

채택된 답변

Star Strider
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
endTime = 1000×1 datetime array
10-Jan-2023 11:22:01 10-Jan-2023 11:23:28 10-Jan-2023 11:24:54 10-Jan-2023 11:26:20 10-Jan-2023 11:27:47 10-Jan-2023 11:29:13 10-Jan-2023 11:30:40 10-Jan-2023 11:32:06 10-Jan-2023 11:33:32 10-Jan-2023 11:34:59 10-Jan-2023 11:36:25 10-Jan-2023 11:37:52 10-Jan-2023 11:39:18 10-Jan-2023 11:40:44 10-Jan-2023 11:42:11 10-Jan-2023 11:43:37 10-Jan-2023 11:45:04 10-Jan-2023 11:46:30 10-Jan-2023 11:47:56 10-Jan-2023 11:49:23 10-Jan-2023 11:50:49 10-Jan-2023 11:52:16 10-Jan-2023 11:53:42 10-Jan-2023 11:55:08 10-Jan-2023 11:56:35 10-Jan-2023 11:58:01 10-Jan-2023 11:59:28 10-Jan-2023 12:00:54 10-Jan-2023 12:02:20 10-Jan-2023 12:03:47
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.
.

추가 답변 (2개)

the cyclist
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);

Stephen23
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(:)
ans = 123×1 datetime array
2023-01-10 11:22:01.700 2023-01-10 11:22:01.701 2023-01-10 11:22:01.702 2023-01-10 11:22:01.703 2023-01-10 11:22:01.704 2023-01-10 11:22:01.705 2023-01-10 11:22:01.706 2023-01-10 11:22:01.707 2023-01-10 11:22:01.708 2023-01-10 11:22:01.709 2023-01-10 11:22:01.710 2023-01-10 11:22:01.711 2023-01-10 11:22:01.712 2023-01-10 11:22:01.713 2023-01-10 11:22:01.714 2023-01-10 11:22:01.715 2023-01-10 11:22:01.716 2023-01-10 11:22:01.717 2023-01-10 11:22:01.718 2023-01-10 11:22:01.719 2023-01-10 11:22:01.720 2023-01-10 11:22:01.721 2023-01-10 11:22:01.722 2023-01-10 11:22:01.723 2023-01-10 11:22:01.724 2023-01-10 11:22:01.725 2023-01-10 11:22:01.726 2023-01-10 11:22:01.727 2023-01-10 11:22:01.728 2023-01-10 11:22:01.729
Here is an alternative, simple, numerically robust approach:
t = st+seconds(0.001)*(0:numel(data)-1); % sample times
t(:)
ans = 123×1 datetime array
2023-01-10 11:22:01.700 2023-01-10 11:22:01.701 2023-01-10 11:22:01.702 2023-01-10 11:22:01.703 2023-01-10 11:22:01.704 2023-01-10 11:22:01.705 2023-01-10 11:22:01.706 2023-01-10 11:22:01.707 2023-01-10 11:22:01.708 2023-01-10 11:22:01.709 2023-01-10 11:22:01.710 2023-01-10 11:22:01.711 2023-01-10 11:22:01.712 2023-01-10 11:22:01.713 2023-01-10 11:22:01.714 2023-01-10 11:22:01.715 2023-01-10 11:22:01.716 2023-01-10 11:22:01.717 2023-01-10 11:22:01.718 2023-01-10 11:22:01.719 2023-01-10 11:22:01.720 2023-01-10 11:22:01.721 2023-01-10 11:22:01.722 2023-01-10 11:22:01.723 2023-01-10 11:22:01.724 2023-01-10 11:22:01.725 2023-01-10 11:22:01.726 2023-01-10 11:22:01.727 2023-01-10 11:22:01.728 2023-01-10 11:22:01.729
"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)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by