简体   繁体   English

在Matlab图中动态绘制数据

[英]Plotting data dynamically in the Matlab figure

Just to explain what I am facing, I have the following code. 只是为了解释我所面对的,我有以下代码。

ind=(1:10);
A=[sin(ind);cos(ind);tan(ind);sec(ind)]';
plot(ind,A(:,1),ind,A(:,2),ind,A(:,3),ind,A(:,4));

the result looks like this: 结果看起来像这样:

在此处输入图片说明

Now, in my real program, the matrix A gets updated every few seconds with new rows. 现在,在我的真实程序中,矩阵A每隔几秒钟就会更新一次新行。 And I want to update the graph dynamically as soon as I get a new row. 我想在获得新行后立即动态更新图形。 After some googling I realized that I have to use drawnow, but not sure how. 经过一番谷歌搜索后,我意识到我必须使用drawow,但不确定如何使用。

I have the following code as of now. 到目前为止,我有以下代码。

B=A(1,:);
h = plot(B,'YDataSource','B');
for k = 1:size(A,1)
   B=A(1:k,:);
   refreshdata(h,'caller') 
   drawnow
   pause(.25)
end

But I get the following error on this: 但是我得到以下错误:

Error using refreshdata (line 70) Could not refresh YData from 'B'. 使用refreshdata时出错(第70行)无法从“ B”刷新YData。

Error in test (line 9) refreshdata(h,'caller') 测试错误(第9行)refreshdata(h,'caller')

Please help. 请帮忙。

I solved it after some more googling. 经过更多的谷歌搜索后,我解决了它。 The following code does, what I wanted: 以下代码可以实现我想要的功能:

ind=(1:10);
A=[sin(ind);cos(ind);tan(ind);sec(ind)]';
plots=plot(ind(1,1),A(1,1),ind(1,1),A(1,2),ind(1,1),A(1,3),ind(1,1),A(1,4));
for k = 1:size(plots,1)
   set(plots, {'XData'}, {ind(1,1:k);ind(1,1:k);ind(1,1:k);ind(1,1:k)})
   set(plots, {'YData'}, {A(1:k,1);A(1:k,2);A(1:k,3);A(1:k,4)})
   drawnow
   pause(.5)
end

This answer helped me to find the solution : https://stackoverflow.com/a/36155528/919177 这个答案帮助我找到了解决方案: https : //stackoverflow.com/a/36155528/919177

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM