Plotting x-axis time in dd hh mm ss format
조회 수: 106 (최근 30일)
이전 댓글 표시
Hi folks, hope you are well.
I am just wondering if anyone has any experience with gps-type time scales for plotting. I am plotting a series of interactive plots and already have my x parameter (time in seconds) and my y parameters to match. However, I also have a matching time dataset which is in dd hh mm ss format, for day, hour, minute, second - where day started on the 1st january this year (locally).
I would really like to explore the possibility of having my x-axis time scale in the form dd hh mm ss as is given in the data, whilst STILL having the iteractivity component of these plots. i.e. being able to zoom and scale with the time snapping to ajust.
I have looked through plenty answers but can't find much help with what exactly i'm trying to do - hope someone can help!
Many thanks,
C.
댓글 수: 0
채택된 답변
Bora Eryilmaz
2023년 4월 4일
편집: Bora Eryilmaz
2023년 4월 11일
You can use the datetime data type for the x-axis values and adjust the date/time format as you like:
now = datetime;
x = now + seconds(0:10)/7;
y = rand(1,11);
plot(x,y)
ax = gca;
ax.XAxis.TickLabelFormat = 'HH:mm:ss.SSS';
ax.XTickLabelRotation = 45;
t = 4.3860e+04 + (0:100)/pi; % An array of seconds starting at 4.3860e+04 seconds.
ts = seconds(t); % Convert it to duration in seconds.
ts.Format = 'hh:mm:ss.SSS';
y = rand(size(t));
plot(ts,y)
ax = gca;
ax.XTickLabelRotation = 45;
All the zoom, pan, etc, behaviour would continue to be functional.
댓글 수: 5
추가 답변 (1개)
Steven Lord
2023년 4월 5일
Based on the clarifications you posted on the answer by @Bora Eryilmaz I think you want to plot a duration array rather than a datetime array. As an example let's take 10 values randomly generated as a number of seconds between 0 and 3 hours.
s = sort(randi([0 seconds(hours(3))], 1, 10))
Create a duration array from s by calling seconds on it. seconds, minutes, hours, etc. can accept either a duration or a number and converts to the other type. I used it in both senses above; hours(3) to convert a number to a duration and seconds on the duration to returns the number of seconds in 3 hours.
t = seconds(s)
Let's change its Format. This doesn't change the data, just how it's displayed to the user.
t.Format = 'hh:mm:ss'
and now we can plot.
plot(t, 1:10, 'o-')
You can't zoom on the picture here on MATLAB Answers, but if you were to run this code in an interactive MATLAB session you could and you'd see the X axis update.
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!