One other thing potentially worth noting: If I change the y-axis parameter, that data changes on the plot; it is only the offset in the datetime object that does not change.
Plotting with datetime in App Designer
12 次查看(过去 30 天)
显示 更早的评论
I am having a problem plotting data against a datetime object in an app created in App Designer. I started a new app to eliminate as many sources of error as possible, so it's very bare bones. A timetable is loaded from a .mat file via the app. That timetable contains the numeric variable and the datetime object as Time. The Time in the timetable has a timezone of 'Etc/UTC', with times from 11:30 - 13:30. I have the following components:
- A uiaxes component for the plot
- A button to command the plot creation
- A button to change the timezone to 'America/New_York'
- A button to change the timezone back to 'Etc/UTC'
- A button to clear the axes
- A text display to show the first element of the datetime object to verify the time before plotting.
If I click the button to create the plot after the data is loaded, it is created as expected, with times from 11:30 - 13:30. If I then click the button to change the timezone to 'America/New_York', Time(1) is displayed in the text display as 07:30, as expected. If I click the plot button, the plot does not change. This is why I added the "clear axes" button, just to be sure the plot is being redrawn. If I click the clear axes button, the axes object clears and the scale on the x-axis becomes the full day over which the datetime object spans. If I click the plot button again, the same plot appears with times from 11:30 - 13:30, even though the times in the timetable are from 07:30 - 09:30. If I restart the app, change the timezone to 'America/New_York' before plotting, then plot, the TIme on the x-axis correctly displays from 07:30 - 09:30. If I clear the axes and change the timezone to 'Etc/UTC' with the other button and then plot, the times on the x-axis are still from 07:30 - 09:30, even though they are 11:30 - 13:30 in the variable.
Am I missing something obvious here?
To be clear, what I am doing in the app is equivalent to the following from the command line or an m-file.
figure;
plot(Time,Var1);
Time is shown on the plot from 11:30 - 13:30, which matches the values in the variable Time, which have a timezone of "Etc/UTC'. I then type the following to change the timezone:
Time.TimeZone = 'America/New_York';
The contents of the Time variable are shifted as expected to 07:30 - 09:30. I then generate a new plot on the same axes.
plot(Time,Var1);
Time is shown from 07:30 - 09:30.
I can't upload the app, but here is the code from the app.
properties (Access = private)
loadedTimetable
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
inputStruct = load('C:\temp\test.mat','data');
app.loadedTimetable = inputStruct.data;
clear inputStruct;
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
plot(app.UIAxes,app.loadedTimetable.Time,app.loadedTimetable.Var1);
end
% Button pushed function: ChangeTimezonetoUTCButton
function ChangeTimezonetoUTCButtonPushed(app, event)
app.loadedTimetable.Time.TimeZone = 'Etc/UTC';
app.Time1EditField.Value = string(app.loadedTimetable.Time(1));
end
% Button pushed function: ChangeTimezonetoNYButton
function ChangeTimezonetoNYButtonPushed(app, event)
app.loadedTimetable.Time.TimeZone = 'America/New_York';
app.Time1EditField.Value = string(app.loadedTimetable.Time(1));
end
% Button pushed function: ClearaxesButton
function ClearaxesButtonPushed(app, event)
app.UIAxes.cla;
end
end
It seems like I'm missing something simple, but I can't see it. I even put a breakpoint in at the "plot" line, went to the command line and ran:
figure; plot(app.loadedTimetable.Time,app.loadedTimetable.Var1);
It plotted time correctly each time, while the app designer code plotted it incorrectly when I continued after the breakpoint.
Any suggestions are appreciated. Thank you.
4 个评论
回答(1 个)
Benjamin Kraus
2020-4-29
编辑:Benjamin Kraus
2020-4-29
I suspect the issue you are running into is the default reset behavior of UIAxes.
By default, the plot command calls newplot. The behavior of newplot depends on the value of the NextPlot property, which is different on regular axes vs. UIAxes. What this means is that by default the plot command will completely reset a regular axes, but will only delete the children of a UIAxes (but preserve most UIAxes properties). This was done so that you can configure the UIAxes when designing your app, and those properties are not lost when you first plot.
For example:
ax = axes;
ax.Color = 'yellow';
ap1 = plot(ax, 1:10);
ap2 = plot(ax, 1:10);
ux = uiaxes;
ux.Color = 'yellow';
up1 = plot(ux, 1:10);
up2 = plot(ux, 1:10);
With the axes above, the color will be reset when you call plot, but with the UIAxes the color will not be reset. In both cases the lines created by the first plot command will be deleted by the second plot command.
The fix for UIAxes is to set the NextPlot property:
ux = uiaxes;
ux.NextPlot = 'replace';
ux.Color = 'yellow';
up1 = plot(ux, 1:10);
up2 = plot(ux, 1:10);
This will cause the plot command (or the cla command) to reset the UIAxes. Note, this will reset most of the UIAxes properties, including the Title, XLabel, grid lines, etc.
At the moment there is no way to selectively reset just the ruler without resetting the entire axes/UIAxes.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polar Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!