Add new datapoints to graph with timer

11 views (last 30 days)
andres rodriguez
andres rodriguez on 15 Apr 2018
Answered: Ameer Hamza on 20 Apr 2018
Hello everybody,
I'm using a timer to read values from Arduino in realtime. Every 2 seconds a value is read. I save the value in a array in the workspace. I have a figure called 'A' to plot the values. I wanna add the last value read from Arduino to the figure 'A' without plot all the array again, just the last value holding old plot values. Anyone knows how to do?
Thanks.

Answers (1)

Ameer Hamza
Ameer Hamza on 20 Apr 2018

You can use axes handle to update current plot, without plotting entire vector again. After plotting the first point using plot(), You can do something like this:

ax = gca; % get handle of current axes;
line = get(ax, 'Children') % get handle to line object
line.XData = [line.XData x_new];
line.YData = [line.YData y_new];

Here x_new and y_new are new data points you want to add to the plot. Remember to update XData and YData together otherwise, it may produce a warning.

Community Treasure Hunt

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

Start Hunting!