Add a datestr to duration data to get in datestr format

36 visualizaciones (últimos 30 días)
Jason
Jason el 18 de Dic. de 2025 a las 18:28
Comentada: Jason el 21 de Dic. de 2025 a las 9:22
Hi, I have a graph where Im plotting on the x-axis time by using tic then in a loop making measurements 'Y' and performing tend=toc
I dor ecord the actual date / time at the start too.
dt = datestr(now,'dd-mmm-yyyy HH-MM-SS'); %get current dateTime
Once my loop has finished an I have a plot of all the Y's against their time of measurements (starting at 0), how can I add the "duration" onto the actual datestr, so I can have on the x-axis the actual date time.
thanks
Jason

Respuesta aceptada

Steven Lord
Steven Lord el 18 de Dic. de 2025 a las 18:46
Rather than using date strings or serial date numbers, use datetime arrays.
x = 1:10;
dt = datetime(2025, 12, x);
y = x.^2;
plot(dt, y, 'o-')
If you're adding points to something like an animatedline iteratively, that works too.
figure
h = animatedline(dt(1), y(1), LineStyle = '-', Marker = 'o');
for k = 2:numel(y)
addpoints(h, dt(k), y(k));
end

Más respuestas (1)

Walter Roberson
Walter Roberson hace alrededor de 14 horas
Answering the question as given
NOW = datetime('now');
%here accumulate array of tic/toc values into variable DURATIONS.
%...
dt = datestr(NOW + DURATIONS / (24*60*60), 'dd-mmm-yyyy HH-MM-SS');
However, chances are high that you do not want to plot all DURATIONS values. You probably want something more like
NOW = datetime('now');
%here accumulate array of tic/toc values into variable DURATIONS.
%...
dt = NOW + DURATIONS / (24*60*60);
plot(dt, VALUES);
datetick('x', 'dd-mmm-yyyy HH-MM-SS')
That said... working with datetime() values is typically much nicer.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by