# unable to classify the variable 'F' in the body of the parfor-loop

2 views (last 30 days)
zein on 13 Apr 2022
Commented: zein on 14 Apr 2022
I was trying to reduce the time taken by parfor and I have tried to calculate F (scatteredInterpolant() ) before the parfor loop
F = scatteredInterpolant(x,y,z,data2(1:len),'linear','linear');
Then, I tried to use interpolant object F inside the parfor loop
parfor i=1:size(data,2)
F.Values=data2(1:len)
end
but the following error was generated
unable to classify the variable 'F' in the body of the parfor-loop. For more information ,see parallel for loops in MATLAB "SOLVE VARIABLE Classification issues in parfor-loops

Edric Ellis on 13 Apr 2022
This use of F creates an order depdency between loop iterations. (You're modifying F on each loop iteration). I'm not entirely sure what you're trying to do here, but perhaps you want to do something more like this?
rng('default')
x = -2.5 + 5*rand([50 1]);
y = -2.5 + 5*rand([50 1]);
v = x.*exp(-x.^2-y.^2);
F = scatteredInterpolant(x,y,v);
parfor i = 1:10
% Make a copy of F
Ftmp = F;
% Modify the copy - this is fine
vnew = x.^2 + y.^2 + i;
Ftmp.Values = vnew;
out(i) = F(i, i);
end
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 2).
disp(out)
0.1631 -0.0129 -0.0522 -0.0989 -0.1457 -0.1924 -0.2392 -0.2859 -0.3327 -0.3794

### More Answers (1)

zein on 13 Apr 2022
Edited: zein on 13 Apr 2022
Thanks for your help, I really appreciate it @Edric Ellis
I have already tried to modify the lines according to your recommendation in your comment, but I got the same error message.
I have also attached the M file and raw file data, The matlab code use to running the code
line 57 where the scatterinterpolation is done
F = scatteredInterpolant(x,y,z,data2(1:len),'linear','linear');
Then, the parfor loop
The F object is called in line 94-95
parpool(8);
%FF=zeros
%parfor (i=1:size(data,2),4)
parfor i=1:size(data,2)
%for i=1:size(data,2)
filename=[surfacename,num2str(sub),var,num2str(file_no(i)),'.raw'];
fullname=fullfile(Dir,filename);
fid = fopen(fullname,'rb'); % rb = read binary
NN=N*10;
fclose(fid);
start=8;
data2=data2(start:length(data2));
len=length(data2)/no_variables;
if opt==1 ||opt==6;
data(:,i)=data2(1:len);
elseif opt==2;
data(:,i)=data2(len+1:2*len);
elseif opt==3;
data(:,i)=data2(2*len+1:3*len);
elseif opt==4;
data(:,i)=data2(3*len+1:4*len);
elseif opt==5;
data(:,i)=data2(4*len+1:5*len);
elseif opt==7;
%F.Values=data2(1:len)
%F = scatteredInterpolant(x,y,z,data2(1:len),'linear','linear');
Ftmp=F;
Ftmp.Values=data2(1:len)
data_int=Ftmp(xq,yq,zq);
data_int_2= reshape(data_int,[],1);
end
end
##### 2 CommentsShowHide 1 older comment
zein on 14 Apr 2022
That solves the problem, thanks a lot for you help @Edric Ellis

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!