Increasing the X axis precision while using times

10 vues (au cours des 30 derniers jours)
Noob Tubem
Noob Tubem le 4 Fév 2021
Commenté : Steven Lord le 4 Fév 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.

Réponse acceptée

Adam Danz
Adam Danz le 4 Fév 2021
Modifié(e) : Adam Danz le 4 Fév 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 commentaires
Adam Danz
Adam Danz le 4 Fév 2021
Good call.
Steven Lord
Steven Lord le 4 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by