run different times in the for loop

6 visualizaciones (últimos 30 días)
Mohamed Afify
Mohamed Afify el 19 de Jul. de 2021
Respondida: Shravan Kumar Vankaramoni el 29 de Jul. de 2021
Hello, I was running s simple for loop with a timer of 1 second as shown below:
r = ratecontrol(1);
n = [1 2 3 4 5]
for v = n
display(v)
waitfor(r);
end
Then I wanted to run parts of the array in different times, I though I would just make another array and run two for loops but MATLAB is single threaded.
what if I have three different arrays:
x = [1 2 3 4]
y = [10 3 6]
z = [3 8 0]
and I want to run X in a 1 second interval, y in 5 second interval and z in 10 second interval, but all running in same for loop in the same time?

Respuestas (1)

Shravan Kumar Vankaramoni
Shravan Kumar Vankaramoni el 29 de Jul. de 2021
Hi,
rateControl cannot be applied here. It executes only at a fixed frequency. You can use pause(t) to execute the loop at different fequencies. Below codes demonstrates the same
n = [1 2 3 4 5];
delays = [1 1 2 3 3];
count = 1;
for x = n
tic
disp(x);
pause(delays(count));
count = count + 1;
toc
end
Refer these links:

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by