hello everyone .can anyone help me and explain to me this codes for real time ploting????please????

arduino=serial('COM10','BaudRate',9600);
fopen(arduino);
CM(1)=0;
time(1)=0;
i=1;
tic;
while (toc<=100)
CM(2)=fscanf(arduino,'%f');
time(2)=toc;
figure(1);
grid on;
axis([toc-10, toc+10, 0, 35])
h(i)=plot(time,CM,'b','LineWidth',5);
hold on
CM(1)=CM(2);
time(1)=time(2);
if(i >= 300)
delete(h(i-299));
end
i=i+1;
end
fclose(arduino); % end communication with arduino

3 Comments

What specifically don't you understand/follow?
Other than it isn't very optimal in that it has a lot of the pieces that are invariant and repetitive inside the loop instead of setting up the figure and axes once before starting the loop, it seems to be pretty straightforward.
For such a purpose, the better choice for plotting would be the new(ish) <animatedline> instead of plot().
Bleh. That code should be rewritten to use animatedline()
I DONT UNDERSTAND the meaninig of i and i=i+1
and this line
h(i)
if(i >= 300)
delete(h(i-299));
end

Sign in to comment.

Answers (1)

As the code runs, data points come in, at times that are not going to be strictly regular (and might possibly be irregular.)
As each new point comes in, the code draws a line segment from the previous point to the new point, and it shifts the axis limits to show 10 seconds before and after the current time. It records the line handles, and after it has accumulated 300 line handles, as it goes it deletes the one that was created 299 ago. So it creates handle #300, deletes handle #1; creates handle #301, deletes handle #2, and so on.
This is all inefficient, and I would recommend that it be replaced with code that creates an animatedline with a limit of 300 points, and then each time a point comes in, uses addpoints() to add the new point to the animated line.

6 Comments

What about it? It's an uncounted loop construct; have to have the index to the handles array somehow the way this was (crudely) constructed.
Any expression of the form
variable = expression
means that the value of the expression is to be computed first, and that it is to replace the current content of the variable.
i = i + 1;
means that the current value of i is to be looked up, and that one is to be added to it, and that the result is to overwrite the variable i
The variable i is being used to count the number of data points that have come in.
And, part of the inefficiency in the above code is in that use
...
h(i)=plot(time,CM,'b','LineWidth',5);
...
if(i >= 300)
delete(h(i-299));
end
i=i+1;
...
The array h of line handles is not preallocated so it is dynamically reallocated with each new point plotted and continues to grow unabated.
Whlle the line handles themselves are deleted when they become aged by 300 successive points, the array itself continues to grow without bound.
While this isn't much memory usage of a single array element, it will eventually begin to show some effects of the reallocation if the data collection were to run for a long time.
It is not good code. It would be much better to rewrite with animatedline()
Indeed. My first comment ended with precisely the same conclusion/recommendation.

Sign in to comment.

Categories

Products

Release

R2020b

Tags

Asked:

on 28 May 2022

Commented:

dpb
on 30 May 2022

Community Treasure Hunt

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

Start Hunting!