Running two for loops in Matlab app

4 views (last 30 days)
Karina Puente
Karina Puente on 17 Aug 2023
Answered: Karan Singh on 28 Aug 2023
I am trying to display data and video at the same time in Matlab app.
My parfor loop is not able to function because the previous for loop need to run before running the next one.

Answers (1)

Karan Singh
Karan Singh on 28 Aug 2023
Hey Karina, the “parfor” loop in MATLAB is designed to execute iterations in parallel, distributing the workload across multiple workers or processor cores. Each iteration of a “parfor” loop is treated as an independent task that can be executed concurrently.
However, running iterations in parallel means that the order of execution is not guaranteed. The iterations may execute in a different order compared to a regular “for” loop. This can lead to issues when the execution of one iteration depends on the results of a previous iteration, as in your case where the previous loop needs to run before the next one.
When using a “parfor” loop, MATLAB divides the iterations into smaller tasks that can be executed independently. These tasks are distributed to different workers, and the order of execution can vary depending on the availability of workers and the workload on each worker. As a result, the guarantee of sequential execution is lost.
Even in cases where you have dependencies with each iteration it is recommended to use the “for” loop only.
This is why a regular “for” loop is more appropriate when you need to run the previous loop before the next one in your specific scenario of displaying data and video simultaneously.
If performance is a concern and you have access to parallel computing resources, you could consider restructuring your code to identify independent tasks that can be executed in parallel, while ensuring that dependent tasks are executed sequentially.

Community Treasure Hunt

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

Start Hunting!