Increasing the X axis precision while using times

10 visualizaciones (últimos 30 días)
Noob Tubem
Noob Tubem el 4 de Feb. de 2021
Comentada: Steven Lord el 4 de Feb. de 2021
Hello, I am trying to plot a graph that takes place over a set of times from 00:00 on one day to 18:00 the next day. My code is:
r8s = readmatrix('hhh.csv');
start = datenum('00:00')
finish = datenum('18:37')
x = linspace(start, finish, 2558);
figure(1)
plot(x, (r8s(:,9) - r8s(:,4)))
yline(0)
dateformat=15
datetick('x', dateformat)
grid on
xlabel('Time UTC')
Which gives the times in three-hour increments. I would like them in 15 minute increments if possible, but have no idea how to achieve this, and trying to manually manipulate the Xticks has not worked for me. Any ideas would be appreciated.

Respuesta aceptada

Adam Danz
Adam Danz el 4 de Feb. de 2021
Editada: Adam Danz el 4 de Feb. de 2021
Give this a shot.
I've added/changed 3 lines (see comments) and removed two lines.
Avoid using datenum and use datetime instead.
r8s = readmatrix('hhh.csv');
start = datetime('00:00','format', 'HH:mm'); % Use datetime
finish = datetime('18:37','format','HH:mm'); % Use datetime
x = linspace(start, finish, 2558);
figure(1)
plot(x, (r8s(:,9) - r8s(:,4)))
yline(0)
grid on
xlabel('Time UTC')
set(gca, 'xtick', start : minutes(15):finish) % set ticks at 15 min intervals
But that's going to give you 75 ticks! So you might need to increase the interval or rotate the ticks: xtickangle(90).
  3 comentarios
Adam Danz
Adam Danz el 4 de Feb. de 2021
Good call.
Steven Lord
Steven Lord el 4 de Feb. de 2021
Rather than using linspace to create x, I'd use colon. I'll use a smaller interval so I can actually show the result.
start = datetime('00:00', 'Format', 'HH:mm');
finish = datetime('01:37', 'Format', 'HH:mm');
x = start:minutes(15):finish
x = 1×7 datetime array
00:00 00:15 00:30 00:45 01:00 01:15 01:30
There is one "gotcha" here in that the last element of x is not equal to finish. But is that desirable if finish is not exactly a multiple of the interval away from start?
if x(end) ~= finish
x(end+1) = finish;
end
disp(x)
00:00 00:15 00:30 00:45 01:00 01:15 01:30 01:37
plot(x, (1:numel(x)).^2, 'o-')
And if you don't need the date part of the datetime use a duration array instead.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by