User`s guide

2 Parallel for-Loops (parfor)
2-20
Similarly, you cannot clear variables from a worker's workspace by executing clear
inside a parfor statement:
parfor ii = 1:4
<statements...>
clear('X') % cannot clear: transparency violation
<statements...>
end
As a workaround, you can free up most of the memory used by a variable by setting its
value to empty, presumably when it is no longer needed in your parfor statement:
parfor ii = 1:4
<statements...>
X = [];
<statements...>
end
Examples of some other functions that violate transparency are evalc, evalin, and
assignin with the workspace argument specified as 'caller'; save and load, unless
the output of load is assigned to a variable. Running a script from within a parfor-
loop can cause a transparency violation if the script attempts to access (read or write)
variables of the parent workspace; to avoid this issue, convert the script to a function and
call it with the necessary variables as input or output arguments.
MATLAB does successfully execute eval and evalc statements that appear in functions
called from the parfor body.
Structure Arrays in parfor-Loops
Creating Structures as Temporaries
You cannot create a structure in a parfor-loop by using dot-notation assignment. For
example, in the following code both lines inside the loop generate a classification error.
parfor i = 1:4
temp.myfield1 = rand();
temp.myfield2 = i;
end
The workaround is to use the struct function to create the structure in the loop, or at
least to create the first field. The following code shows two such solutions.
parfor i = 1:4