Parameters imported using assignin not visible to parfor

2 Ansichten (letzte 30 Tage)
Yoav Livneh
Yoav Livneh am 7 Apr. 2014
Kommentiert: Yoav Livneh am 11 Apr. 2014
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.

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 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 Kommentar
Yoav Livneh
Yoav Livneh am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by