MATLAB Answers

# How to make the time invtervals between two index fixed when plotting in real time using function 'tic toc'

1 view (last 30 days)
LuYao Guo on 15 Mar 2021
Edited: LuYao Guo on 15 Mar 2021
Hi everyone,
I meet a problem when I'm trying to plotting some signals in real time from Arduino to Matltb. I use 'tic toc' to get elapsed time and it works. But when I look back to the 'time' output, the intervals between two neighouring index are irregular. But I want the time intervals to be fixed, for example, it recordes index every 0.3 seconds regularly. My code is look like below:
a = arduino()
%some other code here
tic
while ishandle(plotGraph1)
count = count + 1;
time(count) = toc;
%some other code here
end
I understand why that tic toc can lead to such irregular time intervals, so is there any other options I can use to represent the elapsed time with consistent increasement? Thanks a lot for your help!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

KALYAN ACHARJYA on 15 Mar 2021
Edited: KALYAN ACHARJYA on 15 Mar 2021
"But I want the time intervals to be fixed, for example, it recordes index every 0.3 seconds regularly. My code is look like below"
This way?
while condition
%record
pause(0.3)%
end
Tic ​​start is a stopwatch timer, it return time requiremnets for the code execution until toc. Exact Time: Please note, even in the given code, you must calibrate the fraction of tiny fraction of second required to run the loop (excluding pause).
##### 1 CommentShowHide None
LuYao Guo on 15 Mar 2021
Hi Sir, first of all, thank you for answering. I try the code 'pause', and it makes the intervals change less, although they are still not consistent. Could you please explain more about the 'calibration' please? Do I need to calibrate inside the while loop? BTW, in my while loop, I read and plot the data from Arduino, is it because this loop is complex that leads to the probleams of irregular time invertals? Just in case, I put the whole while loop here.
tic
while ishandle(plotGraph1)
count = count + 1;
time(count) = toc;
dat1 = readVoltage(a,'A2');
dat2 = readVoltage(a,'A3');
%moving average filter
dat1_filtered = movmean(dat1,10);
dat2_filtered = movmean(dat2,10);
data1(count) = dat1_filtered(1);
data2(count) = dat2_filtered(1);
set(plotGraph1,'XData',time,'YData',data1);
set(plotGraph2,'XData',time,'YData',data2);
ylim('auto');
axis([time(count)-10 time(count) min max]);
%Update the graph
%pause(delay);
pause(0.33333);
end
toc
Thank you very much.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!