# Unrecognized variable in parfor loop

11 views (last 30 days)
Francesco Cuomo on 11 Oct 2021
Commented: Francesco Cuomo on 22 Oct 2021
Hi, I'm experiencing some troubles with the parfor loop in a code with some innested for loops. It seems that a variable is not initialized in the correct way.
When trying to run this code:
parfor i=1:50
beta=linbeta(i);
HO=M_HO(alfa,beta);
for j=1:50
Id=linId(j);
if (Iy<Id && Id<Iz) && Ix~=Iy && Iy~=Iz
k=((Iy-Ix)*(Iz-Id)/((Id-Ix)*(Iz-Iy)))^0.5;
elseif (Ix<Id && Id<Iy) && Ix~=Iy && Iy~=Iz
k=((Id-Ix)*(Iz-Iy)/((Iy-Ix)*(Iz-Id)))^0.5;
else
k=0;
printf('Not SAM or LAM');
end
K=RF(0,1-k^2,1);
lintau=linspace(0,4*K,50);
%Preallocation in memory of matrices for results storage
M1=nan(length(linphi)); M2=M1; M3=M1; az1M1=M1; az2M2=M1; az3M3=M1; Mx=M1; My=M1; Mz=M1;
for m=1:length(linphi)
phi=linphi(m);
for l=1:length(lintau)
[psi,theta]=tautoangles(Id,I,we,lintau(l)); %Angles PH, not BH
alfaz1=sin(psi)*sin(theta); alfaz2=cos(psi)*sin(theta); alfaz3=cos(theta);
PN=M_BH(phi,theta,psi)*HO*ON; %M_BH(phi,theta,psi)=PH
quat=dcm2quat(PN);
M=SRT(quat(1),quat(2),quat(3),quat(4),t,prop); %In inertial axes
M_H=HO*ON*M; %In H frame
Mx(m,l)=M_H(1); My(m,l)=M_H(2); Mz(m,l)=M_H(3);
M_P=PN*M; %In principal axes
M1(m,l)=M_P(1); M2(m,l)=M_P(2); M3(m,l)=M_P(3);
az1M1(m,l)=alfaz1*M1(m,l);
az2M2(m,l)=alfaz2*M2(m,l);
az3M3(m,l)=alfaz3*M3(m,l);
end
end
az1M1_av(i,j)=mean(mean(az1M1)); az2M2_av(i,j)=mean(mean(az2M2)); az3M3_av(i,j)=mean(mean(az3M3));
Mx_av(i,j)=mean(mean(Mx)); My_av(i,j)=mean(mean(My)); Mz_av(i,j)=mean(mean(Mz));
end
end
This error occurs at the parfor line:
The source code (...) for the parfor-loop that is trying to execute on the worker could not be found.
Caused by:
Unrecognized function or variable 'k'.
Error using remoteParallelFunction (line 84)
Worker unable to find file.
Unrecognized function or variable 'k'.
It seems to me that there is no reason why the variable "k" should not be initialized before being used in the loops. I tried also to preallocate it as empty variable as soon as the parfor loop starts, but the same error occurs. Instead, without parallel computing no error occurs (but running time is incredibly long).
Francesco Cuomo on 22 Oct 2021
Hi,
thanks for your reply. I can't provide the full version of the code, but the problem was solved by evaluating the variables k and K before the parfor loop, in which K(j) is now used in the definition of the variable lintau.
Thank you.

R2021a

### Community Treasure Hunt

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

Start Hunting!