0
votes

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
Could it be because of your clear all statement after calling main? Trying commenting it out and see if it works. - am304
Also, it's probably better to have the parfor loop outside of the main function if possible and passing the call to main inside the parfor loop. - am304
@am304: No, this should not be the reason and does not help (just tried it). Because the loop script should be executed sequentially, thus until the main call is finished the clear all should not be executed. The parfor should remain in the main function, I do not see another reasonable way right now. - Xolair
@am304: I actually tried to use the parfor inside the loop script, while removing the parfor in the main. This yields the same error. - Xolair
OK, another thought: I assume main is defined with its own file main.m. Maybe try addAttachedFiles with main.m to pass the required MATLAB file to the workers. Maybe the issue isn't about e but about main, despite the error message. - am304

1 Answers

0
votes

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