MATLAB Answers

plot Datetime on x Axes

70 views (last 30 days)
MoHa on 9 Mar 2020
Hello! I have a endless while loop, that generates every 20 min a number. I'm trying to figure out how to plot the data with the dates on the x axis and numbers on the Y.
how can i plot the date and time on x axes?
I'm pretty new to Matlab, would really appreciate any help!


Sign in to comment.

Answers (1)

Benjamin Großmann
Benjamin Großmann on 9 Mar 2020
There are mainly 2 possibilities
  1. You can use datetick() to format your x axis ticks to date or time values
  2. You can define your x-values as datetime() and the plot command automatically formats your x-axis with dateticks
For a mwe, we can assume that we start "now" and are collecting data for 12 hours resutling in a time vector t
t = datetime('now'):minutes(20):datetime('now')+hours(12);
y = rand(size(t));
plot(t, y)
You can also append t with the current time
t = []; % initialization
% ...
% ...
% ...
% every iteration / callback
t(end+1) = datetime('now');
As you are new to matlab, i would also recommend not to use a "endless while loop, that generates every 20 min a number". Have a look at the timer class. With the timer class you can define an instance of the timer which calls a function (schedules) every 20 minutes "in the background". Please let me know if you need help with that.


Show 1 older comment
Benjamin Großmann
Benjamin Großmann on 9 Mar 2020
Let's use a simple example as introduction:
close all
t = timer; % timer instance
t.ExecutionMode = 'fixedRate'; % can be 'singleShot', 'fixedRate', 'fixedDelay', 'fixedSpacing'
t.Period = 0.1; % Period in seconds
t.StartFcn = @myTimerInit; % function which is evaluated at start of timer --> initialization
t.TimerFcn = @myTimerFunction; % function which is evaluated every period
t.start % start the timer#+
% use t.stop to stop the timer, timerfindall to find all timers and delete(timerfindall) to delete all timers
The StartFcn
function myTimerInit(src, evnt)
% some initial values, could also be datetime.empty, but then plotting
% makes some problems
src.UserData.t = datetime('now');
src.UserData.y = 0;
src.UserData.fig = figure('CloseRequestFcn', {@myCloseRequestFcn, src}); = axes('Parent', src.UserData.fig);
src.UserData.p = plot(, src.UserData.t, src.UserData.y);
The TimerFcn
function myTimerFunction(src, evnt)
src.UserData.t(end+1) = datetime('now'); % you can also do some windowing etc. to limit the length of the vector
src.UserData.y(end+1) = rand;
src.UserData.p.XData = src.UserData.t;
src.UserData.p.YData = src.UserData.y;
The close request of the figure to stop the timer before closing the figure
function myCloseRequestFcn(src, evnt, mytimer)
if isvalid(mytimer)
MoHa on 9 Mar 2020
thanky again for your Answer,
sorry i didnt get how can i use them in my code. where can i set the Time interval?
beacuse i define it (Time interval) in my gui (pls. see Pic).
and how can i set parameter (St3d_Dist) for y axes which i get in every iteration?
really appreciate for your solution!
Benjamin Großmann
Benjamin Großmann on 9 Mar 2020
You can set the timer interval via the period property of the timer class. See line 9 in my first code snippet.
First, write a function and put everything inside the function which should be evaluated with the specified rate. This is going to be your timerFcn. Please note that a timer fcn (and other callbacks) must have an input argument for the source and event. You can add additional arguments and pass values to those callbacks like i did with the myCloseRequestFcn().
If you want to store data, you can use the UserData of the timer class as I did with the x data, y data and so on.
Then you just have to adapt the definition of the timer instance from my first code snippet.
If you can post a mwe of your problem, than I can try my best to support you with the timer function.

Sign in to comment.

Sign in to answer this question.




Translated by