.TimeZone data not applying to plot

2 views (last 30 days)
Timothy Sick
Timothy Sick on 10 Feb 2023
Edited: Benjamin Kraus on 17 Mar 2023
I'm working in App Designer. I have a plot with time of day on the x-axis and an input above the plot for time zone. When I start the app up, it will display the plot with the correct time zone information. But as I iterate through code after I've changed the time zone it will not display correctly. It just stays on the time zone input when the app started.
In debug mode I can separately plot the same time_table vs y-axis data and show the correct times, but it will not plot it in the app correctly. The only way I have gotten it to work is to clear the axes and plot it again. However this is very clunky and slows down/crashes the app sometimes. It also messes up the formatting.
I can paste my code here, but it's super lengthy and I honestly don't know where the issues are. I'll paste the function i'm debugging.
SunElevationAzimuthGenerateTable2 - function generates the time_table in UTC time with the TimeZone applied.
function PlotSun(app, LatLonAlt)
if app.GPSPositionCheckBox.Value == 1
Lat = LatLonAlt.LLA(1);
Lon = LatLonAlt.LLA(2);
Alt = LatLonAlt.LLA(3);
time= app.gpsData.GPSReceiverTime;
time.TimeZone = 'UTC';
else
Lat = app.LatEditField.Value;
Lon = app.LonEditField.Value;
Alt = app.AltEditField.Value;
time= datetime("now","TimeZone","UTC");
end
[Sun_Elevation, Sun_Azimuth, time_table, Sun_Elevation_Now, Sun_Azimuth_Now, sunrise_time, sunset_time] = ...
SunElevationAzimuthGenerateTable2(Lat,Lon,Alt,time,app.TimeZoneEditField.Value);
time_table.TimeZone = num2str(app.TimeZoneEditField.Value,'%+.0f');
plot(app.ElevationPlot,time_table,Sun_Elevation);
plot(app.AzimuthPlot, time_table,Sun_Azimuth);
xtickformat(app.ElevationPlot,'HHmm z');
xtickformat(app.AzimuthPlot,'HHmm z');
app.SunElevationEditField.Value = Sun_Elevation_Now;
app.SunAzimuthEditField.Value = Sun_Azimuth_Now;
ylim(app.ElevationPlot,[-10 ceil(max(Sun_Elevation)/10)*10]);
xlim(app.ElevationPlot,[(sunrise_time - 1/24) (sunset_time + 1/24)]);
xlim(app.AzimuthPlot ,[(sunrise_time - 1/24) (sunset_time + 1/24)]);
app.ElevationPlot.Title.String = 'Sun Elevation vs Time (UTC' + string(sprintf('%+.0f',app.TimeZoneEditField.Value)) + ')';
app.AzimuthPlot.Title.String = 'Sun Azimuth vs Time (UTC' + string(sprintf('%+.0f',app.TimeZoneEditField.Value)) + ')';
end
end

Answers (2)

Aishwarya Shukla
Aishwarya Shukla on 3 Mar 2023
Hi @Timothy,
It sounds like the issue is that the plot is not updated correctly when the time zone input is changed. One way to solve this is to use the "hold" command to ensure that the old plot is cleared before the new plot is created. You can add the following code before the plot command:
cla(app.ElevationPlot);
cla(app.AzimuthPlot);
This will clear the existing plots before the new data is plotted, ensuring that the correct data is displayed. Here is the modified code:
function PlotSun(app, LatLonAlt)
if app.GPSPositionCheckBox.Value == 1
Lat = LatLonAlt.LLA(1);
Lon = LatLonAlt.LLA(2);
Alt = LatLonAlt.LLA(3);
time= app.gpsData.GPSReceiverTime;
time.TimeZone = 'UTC';
else
Lat = app.LatEditField.Value;
Lon = app.LonEditField.Value;
Alt = app.AltEditField.Value;
time= datetime("now","TimeZone","UTC");
end
[Sun_Elevation, Sun_Azimuth, time_table, Sun_Elevation_Now, Sun_Azimuth_Now, sunrise_time, sunset_time] = ...
SunElevationAzimuthGenerateTable2(Lat,Lon,Alt,time,app.TimeZoneEditField.Value);
time_table.TimeZone = num2str(app.TimeZoneEditField.Value,'%+.0f');
cla(app.ElevationPlot);
cla(app.AzimuthPlot);
plot(app.ElevationPlot,time_table,Sun_Elevation);
plot(app.AzimuthPlot, time_table,Sun_Azimuth);
xtickformat(app.ElevationPlot,'HHmm z');
xtickformat(app.AzimuthPlot,'HHmm z');
app.SunElevationEditField.Value = Sun_Elevation_Now;
app.SunAzimuthEditField.Value = Sun_Azimuth_Now;
ylim(app.ElevationPlot,[-10 ceil(max(Sun_Elevation)/10)*10]);
xlim(app.ElevationPlot,[(sunrise_time - 1/24) (sunset_time + 1/24)]);
xlim(app.AzimuthPlot ,[(sunrise_time - 1/24) (sunset_time + 1/24)]);
app.ElevationPlot.Title.String = 'Sun Elevation vs Time (UTC' + string(sprintf('%+.0f',app.TimeZoneEditField.Value)) + ')';
app.AzimuthPlot.Title.String = 'Sun Azimuth vs Time (UTC' + string(sprintf('%+.0f',app.TimeZoneEditField.Value)) + ')';
end
This should ensure that the plots update correctly when the time zone input is changed, without the need to clear the axes and re-plot the data.

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.
Prior to R2023a, there was no way to change that time zone without clearing your axes and starting over.
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.
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');

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!