Global and persistent variables behave differently in
parfor
loops than in for
loops.
Because parfor
loops run on many different MATLAB workers
simultaneously, there are many different active workspaces and global contexts.
Some assignments to global or persistent variables inside
parfor
loops affect only the value of the variable on
the MATLAB client. Some assignments do not affect either the value of the
variable on the MATLAB client or the value of the variable on the MATLAB
workers.
In other words, the behavior of global and persistent variables within
parfor
loops can be surprising and using these
variables inside parfor
loops should be avoided.
Change the program logic to not assign to global or persistent variables
inside a parfor
loop. For example, you can do either of the following:
Copy the global
or persistent
variable to a temporary variable before the parfor
loop begins, and then copy it back to the global
or
persistent
variable after the
parfor
loop ends.
Rewrite the code so that it does not use global
or
persistent
variables.