How to make a "moving" graph for a real time signal along the x-axis?
16 views (last 30 days)
Show older comments
Jeff Bryan
on 16 Mar 2017
Commented: Bala Naga Jyothi V
on 22 Jul 2021
Hi All,
I have a piece of code that simulate the plotting of a signal in real time up to 1000 point.
% the data
np=1000;
% prepare the plot
x=1:np;
y=-inf*ones(size(x));
lh=line(x,y);
shg;
% Plot data live
for i=1:np
ix=rem(i-1,np)+1;
y(ix)=.10*fix(i/np)+rand; % <- new data
set(lh,'ydata',y);
pause(.0001);
end
I would like to seek your help on how to make the data "move" along the x-axis. Foe example, after the graph plot from 0 to 50 point. The origin 0 change to start from 51 to 100. Then 100 change to 101. I would like the plot to "Scrolled" towards the left. The purpose of me implementing this function is because the data is getting "cramp up" for displaying all the data from 0 to 1000. I think my description is not very good. I had included a youtube link which is similar to what that I would like to achieve.
https://www.youtube.com/watch?v=d3B4akZiHgo Thank You!
1 Comment
John BG
on 16 Mar 2017
you may consider axis shifting x1, dx y1 y2 constant
axis([x1 x1+dx y1 y2])
and apply drawnow after each plot so the graph is updated
Accepted Answer
John BG
on 16 Mar 2017
Edited: John BG
on 19 Mar 2017
Hi Jeff
you may consider axis shifting x1, dx y1 y2 constant, with
axis([x1 x1+dx y1 y2])
and apply drawnow
for ..
plot(..);drawnow
end
after each plot in the loop so the graph is updated accordingly.
A simple implementation is:
clear all;clc
x=[0:.01:16]
y=sin(3*x)
figure(1);hold all
Dx=50;y1=-1.2;y2=1.2;
for n=1:1:numel(x)
plot(x,y);axis([x(n) x(n+Dx) y1 y2]);drawnow
end
The link supplied by Jan Simon is a search result in the MATLAB exchange, some of the results are static, there are sliders on plots, I understood that you want it shifting, like the YouTube example you mention in the question, correct?
If you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer
please click on the thumbs-up vote link
thanks in advance
John BG
2 Comments
Caleb Schear
on 23 Dec 2020
this works fine, is there any way to make the code halt when I click exit, it just keeps running until the X axis reaches 16
More Answers (3)
Jan
on 16 Mar 2017
This is a common problem and as usual several people have posted solutions in the FileExchange:
0 Comments
Risith Ravisara
on 27 Sep 2017
hear is the code
T = 500;
passo = 1;
t=1;
x=0;
while 1
b=analogRead(a,0);
x=[x,b];
plot(x);
%pay attention to this command %
axis([T*fix(t/T),T+T*fix(t/T),0,1024]);
grid
t=t+passo;
drawnow;
end
0 Comments
Caleb Schear
on 23 Dec 2020
what I did was just put
xlim([(i-10) i])
after the pause function
it worked but if it moves too fast change the pause time or enlarge the i-n value (I used 10 it can be much larger)
1 Comment
Bala Naga Jyothi V
on 22 Jul 2021
I am looking to track the realtime moving object to plot the history data.
But nothing is working either drawnow,etc..,
function setax(Position, srf_in)
figure(1)
hold off
srf_in = srf_in + repmat([Position(1) Position(2) Position(3)],[16 1])
h=plot3(srf_in(:,1),srf_in(:,2),srf_in(:,3),'b',"MarkerSize",10);
% h.Color = 'red';
hold on
ax = gca;
ax.FontSize = 12;
ax.TickDir = 'out';
ax.TickLength = [0.02 0.02];
ax.XLim = [-50 50];
ax.YLim = [-50 50];
ax.ZLim = [-50 50];
addpoints(h,Position(1), Position(2),Position(3));
drawnow
% ax.XLim = [-110 110];
% ax.YLim = [-110 110];
% ax.ZLim = [-110 110];
xlabel('X (m)');
ylabel('Y (m)');
zlabel('Z (m)');
grid on
% end
end
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!