Why am I Unable to use Datetime Ticks for UIAxes in MATLAB App Designer?

19 Ansichten (letzte 30 Tage)
I am working on creating a tool that collects data and plots the data vs. time in real time. I am trying to plot the x-axis as a datetime value. The data is formatted as a datetime array. However, when I pass the data as part of the plot() function, I get an error saying the data must match the numeric format of the x-axis. I did some searching online and saw suggestions using the datetick() or xtickformat() functions to format x-axis as a datetime. I am still getting an error: Error using xtickformat (line 34)Invalid numeric tick label format.Error in Wifi_Performance_Tool (line 643) runStartupFcn(app, @startupFcn). Is it possible to format the x-axis as a datetime.
Here is my startupfcn code:
try
app.loadState;
catch
end
xtickformat(app.UIAxes,'MMM d, yyyy HH:mm:ss a');
while isvalid(app.UIFigure)
if strcmp(app.Switch.Value,'On')
status = readData(app);
if status
writeAPFields(app);
writeCurrentConditions(app);
moveGauge(app);
plotData(app);
drawnow;
pause(0.5);
else
plotData(app);
drawnow;
pause(1.0);
end
else
pause(1.0);
end
end
Here is my UIAxes properties:
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
xlabel(app.UIAxes, 'Time')
app.UIAxes.PlotBoxAspectRatio = [2.63428571428571 1 1];
app.UIAxes.FontWeight = 'bold';
app.UIAxes.XTick = [];
app.UIAxes.BoxStyle = 'full';
app.UIAxes.FontSize = 16;
app.UIAxes.Position = [39 19 1289 521];
Here is the code I am using to plot one line:
hold(app.UIAxes,'on');
if app.RSSICheckBox.Value
plot(app.UIAxes,datenum(app.Datetime_all),app.RSSI_all,'-c',"Marker","o","MarkerFaceColor","cyan","Visible","on")
else
plot(app.UIAxes,datenum(app.Datetime_all),app.RSSI_all,'-c',"Marker","o","MarkerFaceColor","cyan","Visible","off")
end
hold(app.UIAxes,'off');
drawnow;
Also, if anyone can hep, I have check boxes in the toll that should make each line visible or hidden depending on its value. However, the plot becomes visible when I select the checkbox, but when I deselect the box the plot does not become hidden, it stays visible. Any help on these issues would be much appreciated
  1 Kommentar
dpb
dpb am 16 Jun. 2021
plot(app.UIAxes,datenum(app.Datetime_all),app.RSSI_all,'-c',"Marker","o","MarkerFaceColor", ...
will create a NumericRuler x-axis, not a DatetimeRuler. Hence, if you then try to plot a datetime value you'll get the error it must be numeric.
I've got to run right now so can't test, in earlier releases, once you created a numeric axis it was very difficult to turn it into a datetime one; you needed to make the very first plot/creation with a datetime value, even if it was a dummy to create the axes object.
I do not know if it's easier with recent version, but ensure that if you use datetime, use it EVERYWHERE including the first and every time you try to plot.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Jun. 2021
It is possible to plot datetime in app designer.
dt = datetime('now') - days(1:5)
r = rand(1,5)
fig = uifigure()
ax = uiaxes(fig)
plot(ax, dt, r)
What is not possible is to plot datetime data on an axes that already has non-datetime plotted on it.
hold(app.UIAxes,'on');
You should debug at that point to see if app.UIAxes already has plotted information.
  4 Kommentare
Nick Nauman
Nick Nauman am 17 Jun. 2021
Thank you very much for all the help; I appreciate it a lot (i'm not very familiar with matlab app designer yet)! I added a line before my while loop to plot a datetime random series like the code you presented, but I made the line hidden. This allowed me to plot all other datetime lines without any error. I do have one more question that I'm hoping you can answer that isn't exactly related to the datetime series. My gui has checkboxes that are meant ot control if the line is visible or hidden on the uiaxes plot. When the app starts, all lines are hidden and when I select a checkbox the line appears in the uiaxes. However, when I deselect the checkbox, the line does not become hidden. This is the plot code I am using (same code for all 7 lines)
%app.SNRPlot = plot(app.UIAxes,app.Datetime_all,app.SNR_all,'-m',"Marker","o","MarkerFaceColor","magenta");
if app.SNRCheckBox.Value
hold(app.UIAxes,'on');
plot(app.UIAxes,app.Datetime_all,app.SNR_all,'-m',"Marker","o","MarkerFaceColor","magenta","Visible","on")
hold(app.UIAxes,'off');
else
hold(app.UIAxes,'on');
plot(app.UIAxes,app.Datetime_all,app.SNR_all,'-m',"Marker","o","MarkerFaceColor","magenta","Visible","off")
hold(app.UIAxes,'off');
end
drawnow;
I have found the issue to be the hold function keeps me from removing the visible line and replacing it with the hidden line. If I remove the hold function from around the plot function with "Visible","off" property then when I am running the plot in realtime, the line gets redrawn everytime causing the x-axis to resize to what it was at app startup. This causes a flickering that is not visually appealing. I tried every next plot property (since I am not familiar with what each one does), but the visible line does not change to hidden. I also did not have luck with assigning a line object and changing the visibility property everytime the checkbox value changes. Is anyone aware of a good solution to this problem? Any advice is much appreciated.
Walter Roberson
Walter Roberson am 17 Jun. 2021
You should keep a copy of the handles of the line() objects returned from plot(), and then you should set the handle Visible property on or off as needed, without redrawing it. Only redraw it when the data changes (at most)... better yet if the data changes, set the XData or YData properties to update the data without using plot() again.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by