0

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:

enter image description here

Now, in my real program, the matrix A gets updated every few seconds with new rows. 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.

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'.

Error in test (line 9) refreshdata(h,'caller')

Please help.

vipin
  • 1,610
  • 3
  • 16
  • 27

1 Answers1

1

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

vipin
  • 1,610
  • 3
  • 16
  • 27
  • 2
    I think you don't need thoses brackets `{` when using `set(plots)`. If you are using MATLAB 2014b or higher, you can use the dot notation : `plots.XData =`. You can also try to use `drawnow limitrate` in order to plot faster without pausing. – oro777 May 29 '19 at 07:22