Increasing the X axis precision while using times

조회 수: 13 (최근 30일)
Noob Tubem
Noob Tubem 2021년 2월 4일
댓글: Steven Lord 2021년 2월 4일
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.

채택된 답변

Adam Danz
Adam Danz 2021년 2월 4일
편집: Adam Danz 2021년 2월 4일
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
Adam Danz
Adam Danz 2021년 2월 4일
Good call.
Steven Lord
Steven Lord 2021년 2월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by