필터 지우기
필터 지우기

Where is the month, day and year string stored in a plot with a datetime axis which shows hours and minutes in the xticklabel string?

조회 수: 2 (최근 30일)
A somewhat specific question which I cannot find the answer for.
Suppose I have some random data measured at different times which I want to plot:
t1 = datetime('2023-04-01 04:00:00');
t2 = datetime('2023-04-01 12:00:00');
t = t1:seconds:t2;
data = rand(length(t),1);
plot(t,data,'.');
ax = gca;
This results in the following plot:
As you can see, this plot results in x tick labels as strings with hours and minutes. But in the bottom right hand corner of the plot, there is also an additional string showing the month, day and year.
Question: Where is this string stored and can it be edited?
I can't find it anywhere in the ax structure variable. The ax.XTickLabel only contains strings with hours and minutes:
>> ax.XTickLabel
ans =
9×1 cell array
{'04:00'}
{'05:00'}
{'06:00'}
{'07:00'}
{'08:00'}
{'09:00'}
{'10:00'}
{'11:00'}
{'12:00'}
I want to edit and customize this string, change its format, (or perhaps get rid of it entirely).
Any help is appreciated.
  댓글 수: 4
Darcy Cordell
Darcy Cordell 2024년 1월 22일
이동: Dyuman Joshi 2024년 2월 12일
I marked @Star Strider's answer as the accepted. Yours doesn't quite do what I was hoping for. Still wondering if there's a workaround to Star Strider's answer for pre-2023b. Cheers.
Dyuman Joshi
Dyuman Joshi 2024년 1월 23일
이동: Dyuman Joshi 2024년 2월 12일
@Darcy Cordell, check this.
You can use datetick for that -
t1 = datetime('2023-04-01 04:00:00');
%I've changed the time
t2 = datetime('2023-04-01 05:00:00');
t = t1:seconds:t2;
data = rand(length(t),1);
plot(t,data,'.');
%Use datetick with the 15th Format Identifier, which corresponds to HH:MM
datetick('x', 15);

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

채택된 답변

Star Strider
Star Strider 2024년 1월 17일
If you have R2023b, you can use the recently introduced xsecondarylabel function —
t1 = datetime('2023-04-01 04:00:00');
t2 = datetime('2023-04-01 12:00:00');
t = t1:seconds:t2;
data = rand(length(t),1);
plot(t,data,'.');
ax = gca;
xsecondarylabel(Visible="off") % New In R2023b
.
  댓글 수: 2
Darcy Cordell
Darcy Cordell 2024년 1월 22일
I don't have 2023b but I guess this is the best answer for those that do. Any workarounds for pre-2023b?
Star Strider
Star Strider 2024년 1월 22일
The only option I can think of is to create a separate cell array of strings using the original cell array of 'XTickLabel' cell array of strings —
t1 = datetime('2023-04-01 04:00:00');
t2 = datetime('2023-04-01 12:00:00');
t = t1:seconds:t2;
data = rand(length(t),1);
figure
plot(t,data,'.');
title('Original')
figure
plot(t,data,'.');
ax = gca;
% get(ax)
xtl = ax.XTickLabel;
ax.XTickLabel = [];
ax.XTickLabel = xtl;
title('Original, Omitting ‘xsecondarylabel’ Without Using That Function')
That works here, as written, however you will need to try it to be certain that it works in your version.
Other variations (using dates instead of only times) might need a few other tweaks to make this approach work. I did not explore that.
If you want to recover the date information to use in a title or some other location, it can be recovered easily from the returned values in:
xt = ax.XTick
since that information remains even after the 'XTickLabel' values are changed. (I checked to be sure.)
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by