Parameters imported using assignin not visible to parfor

3 views (last 30 days)
I am runnig a function that uses a structure with many fields as an input. I use assignin to import all the fields as variables to the function workspace.
When I use a regular for loop the code works fine, but when I use a parfor loop I get an error that the variables are undefined. For example:
Error using parallel_function (line 598)
Undefined function or variable 'Target_size'.
Error stack:
Main>(parfor body) at 110
Target_size is one of the variables that are imported and is the first of these to appear in the parfor loop. It gives the line number where it appears first, inside the parfor loop.
I have noticed that the error only occurs in a function. If I run the code as a script it works fine.
Is there a way around this? Or maybe is there a better way to import many structure fields as variables to the workspace at once?
Thanks.

Accepted Answer

Edric Ellis
Edric Ellis on 11 Apr 2014
PARFOR requires that you pass all variables explicitly - as you have discovered, it does not understand constructs like ASSIGNIN. If your PARFOR loop is inside a function, the best way to proceed would be to pass all the arguments required for that loop as input arguments to the function.
  1 Comment
Yoav Livneh
Yoav Livneh on 11 Apr 2014
Thanks for the response. I figures out this limitation of parfor the hard way. What I ended up doing is calling another function inside the loop, where I can use assignin.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!