MATLAB: I want to draw a chart with time on the X axis, I use the print function

arduino matlab plot serial port app designer

I am using the serial port to read numbers from the arduino encoder. I did drawing a chart in app designer but in the X axis I only have the number of the next displayed number. I would like to see the time after which the number appeared in the X axis. The first number is e.g. 5 and in the x axis its time is 0, the next number 6 in the x axis appeared after 120ms and wants this time on the X axis.
delete(instrfind({'Port'},{app.PORT}))
app.x=serial(app.PORT,'BAUD',9600);
flushinput(app.x);
fopen(app.x); % otwarcie portu szeregowego
for i=1:app.Zakres.Value
drawnow
if app.Z == 1
break
end
h = str2num(fscanf(app.x));
app.aPolozenie.Value = h;
s = [s,h];
plot(app.UIAxes,s);
app.UIAxes.YLim = [ app.Od.Value app.Do.Value];
app.UIAxes.XLim = [0 app.Zakres.Value];
pause(.0001);
app.Zatrzymaj
end
end
I'm sorry for my English
my chart:
PHOTO.JPG
How to change X axis on time?

Best Answer

Before the loop:
start_time = datetime('now');
rel_times = [];
In the loop, after
s = [s,h];
add
dt = milliseconds(datetime('now') - start_time);
rel_times = [rel_times, dt];
and change
plot(app.UIAxes,s);
to
plot(app.UIAxes, rel_times, s);
Note that performance of your application will get worse and worse over time, due to the way that you keep extending the s and rel_time arrays. Consider using a fixed-size buffer. Consider using animatedline() especially with a maximum number of points.