vipin
vipin

Reputation: 1660

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:

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.

Upvotes: 0

Views: 302

Answers (1)

vipin
vipin

Reputation: 1660

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

Upvotes: 1

Related Questions