drawnow: set axis unit to "time"
Show older comments
Hi! I'm presently collecting analog data from an Arduino microcontroller and plotting it using Matlab and drawnow. My problem is that I want to have the "x" axis set to "real time", in seconds instead of a number of measures. The code is the following:
%a = arduino('/dev/tty.usbmodem1421')
xi = 50; x0 = 0; pass = 1; t = 1; x = 0;
while (t<xi)
input0 = readVoltage(a, 'A0');
input1 = readVoltage(a, 'A1');
input_tot = input0 + input1;
x = [x,input_tot];
plot(x);
axis([x0, xi, 0 2]);
grid on
t = t+pass;
if t>25
xi = xi + 1;
x0 = x0 + 1;
end
drawnow;
end
I'm using to analog ports (A0 and A1) and displaying the sum of these. The x axis is the number of the value taken, increasing in time, but I would like it to be seconds of milliseconds. Do you know if this is possible?
Thanks!
Answers (1)
Walter Roberson
on 19 Mar 2017
https://www.mathworks.com/help/matlab/ref/etime.html
You can use clock() to record the start time and the time you take each sample, and use etime to calculate the time difference. Store that difference as a time vector, and
plot(elapsedTime, x)
Perhaps together with datetick()
In newer MATLAB the recommendation would be to use datetime() to record the times, and then subtract the starting time to get a duration object that you could then use as the first parameter of plot()
Categories
Find more on Graphics Performance 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!