User`s guide
Variables and Transparency in parfor-Loops
2-19
Variables and Transparency in parfor-Loops
In this section...
“Unambiguous Variable Names” on page 2-19
“Transparency” on page 2-19
“Structure Arrays in parfor-Loops” on page 2-20
“Scalar Expansion with Sliced Outputs” on page 2-21
“Global and Persistent Variables” on page 2-22
Unambiguous Variable Names
If you use a name that MATLAB cannot unambiguously distinguish as a variable inside
a parfor-loop, at parse time MATLAB assumes you are referencing a function. Then at
run-time, if the function cannot be found, MATLAB generates an error. (See “Variable
Names” in the MATLAB documentation.) For example, in the following code f(5) could
refer either to the fifth element of an array named f, or to a function named f with an
argument of 5. If f is not clearly defined as a variable in the code, MATLAB looks for the
function f on the path when the code runs.
parfor i = 1:n
...
a = f(5);
...
end
Transparency
The body of a parfor-loop must be transparent, meaning that all references to variables
must be “visible” (i.e., they occur in the text of the program).
In the following example, because X is not visible as an input variable in the parfor
body (only the string 'X' is passed to eval), it does not get transferred to the workers.
As a result, MATLAB issues an error at run time:
X = 5;
parfor ii = 1:4
eval('X');
end