Repeating a sequence of code multiple times in a for loop with random intervals in between
1 view (last 30 days)
Show older comments
Emma Plater
on 19 Oct 2020
Commented: Walter Roberson
on 20 Oct 2020
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])
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
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!