Problems with parfor-loop in command line execution

387 views Asked by At

I am trying to execute this piece of code:

parpool('local',4);

range = 1000;

parfor i = 1:range
    A_test(i) = sqrt(i);
end

It runs perfectly in the MATLAB-Desktop window, but when I try to run it from the command line using

matlab -nosplash -nodesktop -r "run script.m"

I get the following error:

Error using onCleanup (line 50)
Not enough input arguments.

Error in parfor_test (line 9)
parfor i = 1:range

Error in run (line 96)
evalin('caller', [script ';']);

I read, that I might have to preallocate the vector A_test, so I modified the code like this:

parpool('local',4);

range = 1000;

A_test = zeros(1,range);

parfor i = 1:range
    A_test(i) = sqrt(i);
end

Now I don't get any errors, but A_test does not get filled with sqrt-numbers, but stays filled with zeros. Do you have any ideas, what I could do to fix this bug?

1

There are 1 answers

4
matlabgui On

You don't need to use run in your command. Just do:

matlab -nosplash -nodesktop -r "script"

edit Just realised that I had a typo in the above command (no need for .m extension -> that will cause an error)