Why am I Unable to use Datetime Ticks for UIAxes in MATLAB App Designer?
14 views (last 30 days)
Show older comments
Nick Nauman
on 16 Jun 2021
Commented: Walter Roberson
on 17 Jun 2021
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 Comment
dpb
on 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.
Accepted Answer
Walter Roberson
on 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 Comments
Walter Roberson
on 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.
More Answers (0)
See Also
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!