Incorrect TimeZone displayed on plot

5 views (last 30 days)
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
  1 Comment
Timothy Sick
Timothy Sick on 13 Feb 2023
Well after 2 weeks of googling/asking questions i figure i'll post how i made it work.
Put this line of code after your entire plot/formatting section.
app.UIAxes.NextPlot = 'replace';
you'll have to do some formatting each iteration, but it fixed my issue.

Sign in to comment.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 17 Mar 2023
Edited: Benjamin Kraus on 17 Mar 2023
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).
You can read about this on the DatetimeRuler Properties doc page.
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
ans = 'America/New_York'
% You can change the time zone by setting the ReferenceDate on the XAxis.
ax.XAxis.ReferenceDate = datetime(2023,2,10,'TimeZone','Europe/London');

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!