0

In the following parfor loop, Matlab says the variable 'sf' cannot be classified. However, the way it is defined inside the innermost loop doesn't seem to affect parfor. Could you tell me the issue and show me how this code snippet should be modified?

parfor ii=1:1:10000
    for jj=1:200
        for kk=1:80
            sf{kk}=fit([kk*dKy;(kk+1)*dKy],[result{kk}(ii);result{kk+1}(ii)],'exp1','lower',[kk*dKy,result{kk}(ii)]);
            fun=@(t) sf{kk}(t).*cos(Ky(kk).*t);
            result2{ii}(jj)=0;
            result2{ii}(jj)=result2{ii}(jj)+integral(fun,Ky(kk),Ky(kk+1),'ArrayValued',true)/(2*pi);
        end
    end
end
  • `sf` depends on `ii`, which is the parallel index. Each `sf` would need information of all cores, and this is not permited/bad. – Ander Biguri May 16 '18 at 08:43
  • How should I modify this code? Thank you. – A Slow Learner May 16 '18 at 08:47
  • For starters put `result2{ii}(jj)=0;` outside your `kk` loop, since now you set it to 0 every `kk` iteration, then add something. – Adriaan May 16 '18 at 08:48
  • Each iteration needs the complete `result` cell, to extract a single value, `ii`, from two specific cells, `kk` and `kk+1`. Probably this can be done in a smarter way, since you broadcast the full `result` variable to all workers whereas you need only a very small bit. Otherwise, I think MATLAB has a problem with `fit` inside a `parfor` loop, in addition to what Ander said. Read [this post](https://stackoverflow.com/q/32146555/5211833) for some general info on `parfor`. – Adriaan May 16 '18 at 08:51

0 Answers0