1

I am trying to do a dynamic plotting inside a nested loop in Matlab.This is somewhat similar to plot inside a loop.But I have one more loop. This is what my code looks like.

for i = 1:100
  for j = 1:100
    data(i,j) = fun(i,j);
  end
end

plot(mean(data));

So I am plotting the mean data as an array. But for large iterations the plot shows only after the loop ends. Is there any way I can plot the data dynamically so something is plotted while the iteration goes on? How should I write the plot function inside the loops so I can achieve it?

P.S. : Actually there is some other functions running inside the loop the final value that is assigned to data(i,j) comes after some some complex calculations.The jth value that is assigned to data(i,j) is dependent on (j-1)th value and the very first value in the ith loop is some initial value. Also the mean(data) gets plotted which means I have to take the mean of the column data of the matrix which is returned as an array by the function mean(data).

7
  • (1) your plot instruction is outside the loop in your example, so the plot will always be generated after the loop ends. (2) Is that all what your double loop does ? If yes you do not need a double loop but a single statement: data = bsxfun( @times , 1:100 , (1:100).' ) Commented Mar 27, 2016 at 16:23
  • I have edited my question. Sorry for the confusion. Actually I want to to put the plot function suitably inside the loop so that I can get a dynamic plot. But I can't figure it out. Commented Mar 27, 2016 at 16:50
  • Also for large iterations bsxfun will take longer execution time. Commented Mar 27, 2016 at 16:57
  • 1
    Then you can use any of the method described in the question you linked. The fact that you have multiple loop nested does not change anything. Just place the 'plot refresh' part of the code in the loop where you want the update (inner loop or outer loop). I personnally recommend this answer (not the accepted one) which define the plot at the beginning then only change the XData/YData within the loop. Commented Mar 27, 2016 at 17:05
  • 1
    I don't see how this is different from that linked question Commented Mar 27, 2016 at 17:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.