I want to compute a fractal image in two nested loops over the pixel indices (ix,iy). The sample code just assigns a random number to RGB values instead of the real calculations.
x = 0:.2:4;
y = 0:.2:3;
nX = length(x);
nY = length(y);
RenderRed = zeros(nX,nY); RenderGreen = zeros(nX,nY); RenderBlue = zeros(nX,nY);
parfor ix = 1:nX
% for iy = 1:length(y) % error
for iy = 1:nY
% "compute" pixel (ix,iy)
RenderRed(ix, iy) = rand; RenderGreen(ix, iy) = rand; RenderBlue(ix, iy) = rand;
end
end
Pctr = [];
Pctr(:,:,1)=RenderRed; Pctr(:,:,2)=RenderGreen; Pctr(:,:,3)=RenderBlue;
handle = image(Pctr);
shg
The code works as shown but if the end value of the iy loop is changed from nY to length(y) - see the commented line - an error is issued:
Error: The variable RenderRed in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
Why? In my understanding of sliced variables no version should work: one has to use an auxiliary variable collecting the results of the inner loop, and assign that to a slice of the matrix. But length(y) instead of nY should have no effect at all on the classification of variables because y is never assigned in the loops.
mlint finds no error, in neither version. Same behaviour with MATLAB versions 2016b, 2017b.

nYvariable is modified or not in the parfoor loop, so to avoid that one pool modify this variable matlab simply do not give you the right to set the for loop limit using a function call. I imagine that if this code was compiled and not interpreted both solutions could work.