Repeating a sequence of code multiple times in a for loop with random intervals in between

1 view (last 30 days)
I want to repeat the following code 150 times with 500-800 milliseconds in between (i.e. I want the iterations to be spaced apart randomly, with each spacing being between 500 and 800).
rec=@(x) (x)>=0.75
j = (0 : 0.01 : 29);
k = sin(j);
f = rec(k);
[a,b]=find(f~=0);
N=length(b);
s=0.15;
f=double(f);
f(b)=1+s*rand(1,N);
plot(j,f);
axis([0 29 0 2])

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2020
rec=@(x) (x)>=0.75;
for counter = 1 : 150
j = (0 : 0.01 : 29);
k = sin(j);
f = rec(k);
[a,b]=find(f~=0);
N=length(b);
s=0.15;
f=double(f);
f(b)=1+s*rand(1,N);
plot(j,f);
axis([0 29 0 2])
drawnow
delay = 500+300*rand();
pause(delay/1000);
end
  2 Comments
Emma Plater
Emma Plater on 20 Oct 2020
Great, thank you!
How do I then store all iteration loop outputs in a single row? Right now only the result of the last iteration is saved and I'd like to be able to plot all 150 iterations continuously in a single graph.
Walter Roberson
Walter Roberson on 20 Oct 2020
You cannot store delays. Delays are something that happen in real time. MATLAB does not have any way to allow you to submit a list of points to plot with built-in delays at particular points in the output.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!