How to make a "moving" graph for a real time signal along the x-axis?

21 ビュー (過去 30 日間)
Jeff Bryan
Jeff Bryan 2017 年 3 月 16 日
コメント済み: Bala Naga Jyothi V 2021 年 7 月 22 日
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.
  1 件のコメント
John BG
John BG 2017 年 3 月 16 日
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

サインインしてコメントする。

採用された回答

John BG
John BG 2017 年 3 月 16 日
編集済み: John BG 2017 年 3 月 19 日
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 件のコメント
Jeff Bryan
Jeff Bryan 2017 年 3 月 19 日
Thank You very much! It works
Caleb Schear
Caleb Schear 2020 年 12 月 23 日
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

サインインしてコメントする。

その他の回答 (3 件)

Jan
Jan 2017 年 3 月 16 日
This is a common problem and as usual several people have posted solutions in the FileExchange:

Risith Ravisara
Risith Ravisara 2017 年 9 月 27 日
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

Caleb Schear
Caleb Schear 2020 年 12 月 23 日
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 件のコメント
Bala Naga Jyothi V
Bala Naga Jyothi V 2021 年 7 月 22 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by