Parameters imported using assignin not visible to parfor

3 ビュー (過去 30 日間)
Yoav Livneh
Yoav Livneh 2014 年 4 月 7 日
コメント済み: Yoav Livneh 2014 年 4 月 11 日
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.

採用された回答

Edric Ellis
Edric Ellis 2014 年 4 月 11 日
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 件のコメント
Yoav Livneh
Yoav Livneh 2014 年 4 月 11 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by