Matlab: access function input in parfor loop

1.5k views Asked by At

I have a script with a loop going over several different value combinations. This script calls the main with the value combinations, which then calls a parfor which accesses the different values. In the following is a dummy simplification of my code. If needed, I will supply the full code.

Loop:

a = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8];
b = [5,10,15,20,25,30];
c = [0,1];

% Iterate over all possible combinations
for p = 1:length(a)
    for s = 1:length(b)
        for e = 1:length(c)
            main(p,s,e); clear all;
        end
    end
end

Main:

function main (p,s,e)
    parfor k = 1:51
        if(e)
            display('Foobar');
        end
    end
end

So I basically want to decide in the parfor loop what to do (e.g. how to create intervals etc.) with the help of the input parameters. I do not want to edit this parameters, just read and use them.

Now I get the following error:

An UndefinedFunction error was thrown on the workers for 'e'. This might be because the file containing 'e' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details.

I don't get why this does not work. Just defining e again, like e2 = e does not help either.

Greetings

Edit: What actually seems to work is when I pass not the variables of the for loop directly to the main but actually use the arrays like intended. E.g.:

main(a(p),b(s),c(e))
1

There are 1 answers

0
Xolair On

I found the solution. I tried to pass the variables assigned to go through the for loops to the main function. This is not possible. What I actually wanted (and did) now instead is passing a value from the arrays defined before to the main function.

a = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8];
b = [5,10,15,20,25,30];
c = [0,1];

% Iterate over all possible combinations
for p = 1:length(a)
    for s = 1:length(b)
        for e = 1:length(c)
            main(a(p),b(s),c(e)); clear all;
        end
    end
end