How to create a time vector that is incremented between two datetime strings?

22 visualizaciones (últimos 30 días)
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];

Respuesta aceptada

Star Strider
Star Strider el 11 de En. de 2023
Editada: Star Strider el 11 de En. de 2023
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.
.

Más respuestas (2)

the cyclist
the cyclist el 11 de En. de 2023
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 el 11 de En. de 2023
Editada: Stephen23 el 11 de En. de 2023
"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)

Categorías

Más información sobre Array Geometries and Analysis en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by