From the parfor documentation :
The body of a parfor-loop element cannot contain global or constant variable declarations.
In the context of your problem, that is, calling a function in parfor , which, in turn, refers to global , it means: " parfor probably will not give the expected or meaningful results."
It makes sense. Consider the following
Lab 1: Lab 2: GetB(); GetB();
if the contents of GetB() is this:
function GetB() global B; %
What will be the value of B if Lab 1 indicated Lab 1 ? and on Lab 2 ? How are the various rand results conveyed? It will be a mess!
parfor code suitable for parfor loops can be a real pain when this code comes from what only regular for -loops has. As a rule, when you know in advance, you are going to write a computationally intensive code fragment of Matlab, write all functions and loops as parfor , starting from the very beginning. This is the only way that such errors will not cost you money when transcoding your functions.
Converting from for to parfor not generally trivial .
source share