How to use dynamic variable in Parfor
6 views (last 30 days)
Show older comments
I try to use dynamic variables in a parfor Loop. But the function EVAL can not be called within the loop. examples like
sensorNumber= [1361 1364 1367 1368 1373 1381 1382 1401 1565 1567];
for i=1:length(sensorNumber)
eval(['value' num2str(sensorNumber(i)) '= dust' num2str(sensorNumber(i)) ';']);
end
how can i use parfor to process this kind of thing
1 Comment
Answers (1)
Walter Roberson
on 10 Aug 2015
You cannot use parfor to process that kind of thing.
I suggest
parfor i = sensorNumber
value{i} = dust{i};
end
which I would quickly replace with
value(sensorNumber) = dust(sensorNumber);
with no loop.
3 Comments
Steven Lord
on 10 Aug 2015
You cannot and should not. PARFOR needs to analyze the contents of the loop body in preparation for parallelizing it, and I believe that analysis can't be completed if there's an EVAL statement in the loop (since the code to be executed could literally be anything, including something that introduces an ordering to the loop iterations.)
I believe using dynamic field names with a struct array will work in a PARFOR.
Walter Roberson
on 10 Aug 2015
Edited: Walter Roberson
on 10 Aug 2015
Cell arrays indexed by number will work for sure.
Dynamic field names would likely lead to an error about the variable not being classifiable, because the parfor analyzer is not able to interpret the field-name construction code well enough to be able to prove that no field is being duplicated (which would introduce an order dependency)
See Also
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!