How to create a time vector that is incremented between two datetime strings?
26 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Brianna Miranda
il 11 Gen 2023
Modificato: Stephen23
il 11 Gen 2023
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 Commenti
Risposta accettata
Star Strider
il 11 Gen 2023
Modificato: Star Strider
il 11 Gen 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
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 Commenti
Più risposte (2)
the cyclist
il 11 Gen 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);
0 Commenti
Stephen23
il 11 Gen 2023
Modificato: Stephen23
il 11 Gen 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(:)
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 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!