How to create a time vector that is incremented between two datetime strings?
19 次查看(过去 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 中查找有关 Array Geometries and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!