App Designer: Plotting on uiaxes from a callback. Formatting X-axis as HH:mm:ss

5 views (last 30 days)
I have arrays of accelerometer sensor data which have been recorded at 10Hz. The array "time" contains time data as a sequence of seconds with 0.1s resolution: {1.1,1.2,1.3,1.4...etc}. I can plot my acceleration variables (Ax, Ay,Az) on the Y-axis against the X-axis timebase in seconds without a problem.
properties (Access = private)
time
Ax
Ay
Az
The data recordings are usually around 8 hrs long and I would like to format the x-axis in HH:mm.
After reading several helpful comments here, I supposed that app.graph_plot.XTickFormat = 'HH:mm'; but it doesn't work (the code jumps to the "catch" error handler). The only thing which I can get to slightly work is with "datetick".. however MATLAB gives me a warning that it is better to use "xtickformat".
app.CNS_plot = uifigure("Name",'Acceleration Plot','Color','white');
app.graph_plot = uiaxes(app.CNS_plot,'YLimMode','auto','YLim',[-inf,inf]);
app.graph_plot.Toolbar.Visible = 'on';
app.graph_plot.XAxis.Exponent = 0;
app.graph_plot.TickDir = 'out';
datetick(app.graph_plot,'x','HH:mm'); {Matlab warns me that this is not recommended}
app.graph_plot.XLabel.String = 'Time in Seconds';
When the figure is created, the X-axis is formatted as 00:00, 06:00, 12:00, 18:00 so it seems to work, however when I plot the data using
plot(app.graph_plot, app.time, app.Ax,"Color",'black','LineStyle','-','DisplayName','Accel X');
then all formatting disappears. Now I suppose that the issue could be with my "time" array definition. It is not explicitely defined as a 'datetime' or a 'duration' array. I am sure this must be a common problem, and all the answers I have seen so far (using XTickFormat) won't run.
Can anyone please suggest a solution?
many thanks

Accepted Answer

Umar
Umar on 29 Sep 2025

Hi @SteveM,

Thank you for your insightful comments regarding the x-axis time formatting issue.

After reviewing the MathWorks documentation, it’s clear that `xtickformat` works properly only when the x-axis data is a `duration` or `datetime` array. Since my `time` array is numeric (seconds), this explains why `app.graph_plot.XTickFormat = 'HH:mm';` fails and why formatting disappears after plotting.The recommended solution is to convert the numeric time to a `duration` type before plotting:

    time_dur = seconds(app.time);
    plot(app.graph_plot, time_dur, app.Ax, 'Color', 'black');
    xtickformat(app.graph_plot, 'hh:mm');

This approach avoids the deprecated `datetick` function and ensures the HH:mm formatting persists correctly.

Thanks again for your guidance. Let me know if this helped resolved your issue.

  1 Comment
SteveM
SteveM on 29 Sep 2025
Hi Umar,
yes you are right! I think I had come to the same conclusion as your answer arrived! I changed the arrays into "seconds" as you said and now the xtickformat operation works fine. Many thanks.
Steve

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2025b

Community Treasure Hunt

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

Start Hunting!