User`s guide
Objects and Handles in parfor-Loops
2-13
Objects and Handles in parfor-Loops
In this section...
“Using Objects in parfor-Loops” on page 2-13
“Handle Classes” on page 2-13
“Sliced Variables Referencing Function Handles” on page 2-13
Using Objects in parfor-Loops
If you are passing objects into or out of a parfor-loop, the objects must properly
facilitate being saved and loaded. For more information, see “Save and Load Process”.
Handle Classes
You can send handle objects as inputs to the body of a parfor-loop, but any changes
made to handle objects on the workers during loop iterations are not automatically
propagated back to the client. That is, changes made inside the loop are not
automatically reflected after the loop.
To get the changes back to the client after the loop, explicitly assign the modified handle
objects to output variables of the parfor-loop. In the following example, maps is a sliced
input/output variable:
maps = {containers.Map(),containers.Map(),containers.Map()};
parfor ii = 1:numel(maps)
mymap = maps{ii}; % input slice assigned to local copy
for jj = 1:1000
mymap(num2str(jj)) = rand;
end
maps{ii} = mymap; % modified local copy assigned to output slice
end
Sliced Variables Referencing Function Handles
You cannot directly call a function handle with the loop index as an input argument,
because this cannot be distinguished from a sliced input variable. If you need to call a
function handle with the loop index variable as an argument, use feval.
For example, suppose you had a for-loop that performs: