Incorrect TimeZone displayed on plot
조회 수: 8 (최근 30일)
이전 댓글 표시
Working on a simple plot of time of day (hrs - on the x-axis) and arbitrary data on the y-axis in the App Designer. Whatever the initial plotted time zone was, will be the time zone even if I change it.
In this example, I create a time matrix of time starting on 15 March, 2023 from midnight to midnight (24 hrs). The y-data is arbitrary but is the same size as the time matrix.
After running the app, you select one of the "time zone" buttons to set either 'UTC' or 'UTC-8' as the global timezone variable, then you hit plot. After plotting initially, you can change the timezone again by hitting the other button and plotting. However, the timezone on the x-axis remains the initial timezone.
The only way I could fix this is by clearing the plot or axes after each plot. This is very clumsy and slow. What am I missing?
Here is the part of the app I created. I attached the app for others to try out.
properties (Access = private)
timezone % Global timezone variable
end
methods (Access = private)
function PlotFunction(app)
time_init = datetime(2023,3,15);
time_init.TimeZone = app.timezone;
time_addition = hours(0:1:24);
time = time_init + time_addition;
x = 0:1:24;
y = sin(x);
plot(app.UIAxes,time,y)
xtickformat(app.UIAxes,'HHmm z')
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
app.PlotFunction
end
% Button pushed function: SetZuluButton
function SetZuluButtonPushed(app, event)
app.timezone = 'UTC';
end
% Button pushed function: SetUTC8Button
function SetUTC8ButtonPushed(app, event)
app.timezone = '-8';
end
end
채택된 답변
Benjamin Kraus
2023년 3월 17일
편집: Benjamin Kraus
2023년 3월 17일
I believe the issue you are facing is that the axes/ruler has a specific time zone that is initialized when you first call plot. By default, the time zone matches the data that was first used to create the plot. Anything you plot into that axes afteward will display using that time zone, regardless of the time zone of the data that was used.
Prior to R2023a, there was no way to change that time zone without clearing your axes and starting over. In order to clear and start over with UIAxes you need to turn off hold (by setting NextPlot to 'replace' or by calling hold(app.UIAxes,'off')).
Starting in MATLAB R2023a, you can now update the time zone of the x-axis of existing plots by setting the ReferenceDate on the ruler. This no longer requires resetting your axes, so you can leave NextPlot set to the default value (and do not have to redo formatting every iteration).
For example:
d = datetime(2023,2,10,TimeZone='America/New_York') + hours(0:24);
% When you call plot, the axes will be configured to use the
% "America/New_York" time zone.
plot(d,sin(1:numel(d)));
% You can query the current time zone like this:
ax = gca;
ax.XAxis.ReferenceDate.TimeZone
% You can change the time zone by setting the ReferenceDate on the XAxis.
ax.XAxis.ReferenceDate = datetime(2023,2,10,'TimeZone','Europe/London');
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!