Search code examples
matlabparallel-processingparfor

parfor with Matlab "the variable __ in a parfor cannot be classified"


So I am trying to call this function using a parfor (basically curve fitting using Fourier series through a vector in a parfor loop):

function[coefnames,coef] = fourier_regression(vect_waves,n)

    coef = zeros(length(vect_waves)-n,18);
    current_coef = zeros(18,1); % All the terms of the fourier series
    x = 1:n;

    parpool_obj = parpool;
    parfor i=n:length(vect_waves)

        take_fourier = vect_waves(i-n+1:i);

        f = fit(x,take_fourier,'fourier8');
        current_coef = coeffvalues(f);
        coef(i,1:length(current_coef)) = current_coef;

    end
    coefnames = coeffnames(f);

    delete(parpool_obj);
end

But I am just getting the error

"The variable coef in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".

I can't seem to find the solution anywhere, I don't know what the problem is. What's going on?


Solution

  • @Andras Deak put this in a comment, but the fix here is really simple - all you need to do is use a form of indexing for the second subscript to coef that is allowed by parfor. In this case, you need to do:

    parfor i = ...
        coef(i, :) = ...;
    end