1
votes

The following code:

matlabpool('open','local',2)

parfor i=1:5
    proc = System.Diagnostics.Process;
end

results in an error:

Error: MATLAB cannot determine whether "System" refers to a function or variable.

However, when I execute the parfor loop again (after the error), it runs through! I found a couple of similar questions, but I wasn't able to implement the suggested solutions.

MATLAB parfor - cannot determine whether "ModelUtil" refers to a function or variable?

MATLAB using parfor (parallel computing toolbox) and custom packages with +

I can't wrap my mind around why the loop runs the second time. If I then call

matlabpool close

and execute the whole script again, the error appears once again. So it only happens the first time after the pool was initiated. Any ideas?

1

1 Answers

4
votes

This is because any variable or function you use in a parfor loop must be defined explicitly in the code at parsing time. If there is any ambiguity, Matlab opt to throw an error rather than messing up by assuming.

Just define an anonymous function that create the object you want before the parfor loop, then you can use it as will within the parfor loop.

This run fine on my machine (Matlab R2013a):

getSystemProcess = @() System.Diagnostics.Process ;
parfor i=1:5
    proc = getSystemProcess();
end

Read this Matlab chapter for more informations on how the variable/function names are interpreted in a parfor loop: Unambiguous Variable Names