# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

## Create a parfor-Loop

The safest approach when creating a `parfor`-loop is to assume that iterations are performed on different MATLAB® workers in the parallel pool, so there is no sharing of information between iterations. If you have a `for`-loop in which all iterations are completely independent of each other, this loop is a good candidate for a `parfor`-loop. Basically, if one iteration depends on the results of another iteration, these iterations are not independent and cannot be evaluated in parallel, so you cannot easily reprogram the loop into a `parfor`-loop. For more details on independent iterations, see Compare for-Loops and parfor-Loops.

The following example produces equivalent results, with a `for`-loop on the left, and a `parfor`-loop on the right. Try typing each in your MATLAB Command Window:

 ```clear A for i = 1:8 A(i) = i; end A``````A = 1 2 3 4 5 6 7 8 ``` ```clear A parfor i = 1:8 A(i) = i; end A``````Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers. A = 1 2 3 4 5 6 7 8 ```

Each element of `A` is equal to its index. The `parfor`-loop works because each element depends only upon its iteration of the loop, and upon no other iterations. `for`-loops that merely repeat such independent tasks are ideally suited candidates for `parfor`-loops.

 Note   If you have not started a parallel pool, `parfor` creates a pool using your default cluster profile, if your parallel preferences are set accordingly.

You can specify your inputs and outputs in `parfor` loops in different ways.

If you use a function that is not strictly computational in nature (e.g., `input`, `keyboard`) in a `parfor`-loop or in any function called by a `parfor`-loop, the behavior of that function occurs on the worker. The behavior can include hanging the worker process or having no visible effect at all.

When running a `parfor`-loop on a parallel pool, all command-line output from the workers displays in the client Command Window, except output from variable assignments. Because the workers are MATLAB sessions without displays, any graphical output (for example, figure windows from `plot`) from the pool does not display at all.

### MATLAB Path

All workers executing a `parfor`-loop must have the same MATLAB search path as the client, so that they can execute any functions called in the body of the loop. Therefore, whenever you use `cd`, `addpath`, or `rmpath` on the client, it also executes on all the workers, if possible. For more information, see the `parpool` reference page. When the workers are running on a different platform than the client, use the function `pctRunOnAll` to properly set the MATLAB search path on all workers.

Functions files that contain `parfor`-loops must be available on the search path of the workers in the pool running the `parfor`, or made available to the workers by the `AttachedFiles` setting of the parallel pool.