Loop Variable

The loop variable defines the loop index value for each iteration. It is set with the beginning line of a parfor statement:

parfor p=1:12

For values across all iterations, the loop variable must evaluate to ascending consecutive integers. Each iteration is independent of all others, and each has its own loop index value.

The following restriction is required, because changing p in the parfor body cannot guarantee the independence of iterations.

Required (static): Assignments to the loop variable are not allowed.

This example attempts to modify the value of the loop variable p in the body of the loop, and thus is invalid:

parfor p = 1:n
   p = p + 1;
   a(p) = i;
end

You cannot index or subscript the loop variable in any way. The following code attempts to reference a field (b) of the loop variable (p) as if it were a structure. Both lines within the loop are invalid:

parfor p = 1:n
    p.b = 3
    x(p) = fun(p.b)
end

Similarly, the following code is invalid because it attempts to index the loop variable as a 1-by-1 matrix:

parfor p = 1:n
    x = p(1)
end

See Also

More About

Was this topic helpful?