How to use dynamic variable in Parfor

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

Answers (1)

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

Maybe i put a misleading example here. I just wanted to change variable names in Parfor loop in order to perform further analysis more easily. I know EVAL won't work here.
I have variable name like sensor1361; sensor1664;sensor1669. If I do it in EVAL just need to loop the array of sensor numbers but it is sequential, which means matlab will take ages to process. And i have a lot of loops in the code. The variables and processes are quite independent, thus i wanted to try the parfor.
by the way, each variable is N by 2 matrix.
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.
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)

Sign in to comment.

Categories

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

Asked:

on 10 Aug 2015

Edited:

on 10 Aug 2015

Community Treasure Hunt

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

Start Hunting!