Ploting vs. 24hour day time (string that resets to zero and repeats)

조회 수: 3 (최근 30일)
I want to plot some data that looks like this( i.e, plot(time,x).
time= [ 12 14 16 18 20 22 0 2 4 6 8 10 12 14 16 18 20 22 0 2 4 6 8 10]
X= [ 30 32 34 30 29 26 22 20 18 19 20 28 30 32 34 30 29 26 22 20 18 19 20 28]
but looking for best way to keep order of the data as is. Currently I split the data between 0 times and plot on adjacent subplots which looks ugly. Any suggestions appreciated.

채택된 답변

Star Strider
Star Strider 2015년 8월 29일
I suspect this may be what you want to do:
time = [ 12 14 16 18 20 22 0 2 4 6 8 10 12 14 16 18 20 22 0 2 4 6 8 10];
X = [ 30 32 34 30 29 26 22 20 18 19 20 28 30 32 34 30 29 26 22 20 18 19 20 28];
xt = 1:length(time); % X-Ticks
figure(1)
plot(xt, X)
grid
set(gca, 'XTick', xt, 'XTickLabels', time) % Label X-Axis With ‘time’ Vector
Apologise for the delay — hardware crash on my primary MATLAB computer a couple hour ago, so had to install new to this one.
  댓글 수: 3
Star Strider
Star Strider 2015년 9월 6일
My pleasure!
Not silly — it may not be something you would have likely encountered unless you read the documentation on ‘handle graphics’ in some detail.

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

추가 답변 (1개)

dpb
dpb 2015년 8월 29일
편집: dpb 2015년 8월 29일
A) Convert times to a datenumber and plot against it instead. Use datetick to format the axis or use the new time class with builtin support in latest release(s), or
B) Plot versus index instead of actual time and set tick mark labels manually -- not particularly elegant so A) is the better option.
dn=datenum(2015,1,1,12+[0:2:2*length(time)-1].',0,0);
plot(dn,X)
xlim([dn(1) dn(end)]), ylim([15 35])
datetick('x','dd-HH','keeplimits')
Above uses arbitrary start date; it's immaterial to simply show times but datenum needs six values. Note the generation of the two-hour increments from the starting point to match the length of the data vector and that plotting is against the date numbers that are monotonic which solves your problem of "what to plot against?".
As noted, there's a new time class but I don't have latest release so can't demonstrate it...

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by