Parfor loop classification "fix usage of indicated variable" error

I'm running a parfor loop and I'm trying to output an array with the columns representing each parfor iteration, and the row elements are converted to a non-zero value at specific locations for each column index. The non-zero element row index is different for every iteration.
The problem: Unable to classify the variable 'Binarizing_Rowsum' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".
I tried to understand how to fix it by readig the documentation and other answers on the forum but I'm lost. I understand it is considered a sliced variable, however, I don't know how to fix the representation of the variable so that it matchs parfor requirements to get the output I want.
P.S. the correspoding for-loop worked perfectly
Here is the code:
jValues=1:2:length(baseFileNames)-1;
Binarizing_Rowsum=zeros(6100,length(jValues));
LEDdurIndx=NaN(1000,length(jValues));
Light=3000;
parfor idx=1:numel(jValues)
j=jValues(idx);
%Read files
fullFileName=fullfile(thisFolder, baseFileNames{j});
C_data_Array(:,:,idx)= imread(fullFileName,2);
C_data_ArrayMean(:,:,idx)=mean(C_data_Array(:,:,idx),2);
C_data_ArrayStd(:,:,idx)=std(C_data_ArrayMean(:,:,idx));
LEDdetectThresh=(mean(C_data_ArrayMean(:,:,idx),1))+C_data_ArrayStd(:,:,idx);
LEDdurIndx=find(C_data_ArrayMean(:,:,idx)>LEDdetectThresh);
LED_duration_=LEDdurIndx([1,end]); %Updated
LEDinit(idx)=LED_duration(1);%Updated
LEDend(idx)=LED_duration(2);%Updated
Binarizing_Rowsum([LEDdurIndx],idx)=Light; %OUTPUT ERROR: cannot be classified
end

回答(3 个)

BR = Binarizing_Rowsum(:,idx);
BR(LEDdurIdx) = Light;
Binarizing_Rowsum(:, idx) = BR;

5 个评论

@Walter RobersonThank you so much for the quick response. Now I don't have the error message anymore, however, the values are not allocated to the matrix as expected (all is zero), I added three extra lines to the code (%updated), and the values from (LEDdurIdx) are also not allocated. I don't have any error messages so I'm not sure what is missing, please let me know if something in this code doesn't comply with the parfor requirements. Thank you
Please post your current code. You posted updated code, but you are still using Binarizing_Rowsum([LEDdurIndx],idx)=Light which would continue to generate the error message, and since you say you are not longer getting the error message then the implication is that you are not using that code
Please find the code below. The issue is that the NaN and zero preallocated matrix are not changed after the code is executed, they remain NaN and zero. Thank you
jValues=1:2:length(baseFileNames)-1;
Binarizing_Rowsum=zeros(6100,length(jValues));
LEDdurIndx=NaN(1000,length(jValues));
Light=3000;
parfor idx=1:numel(jValues)
j=jValues(idx);
%Read files
fullFileName=fullfile(thisFolder, baseFileNames{j});
C_data_Array(:,:,idx)= imread(fullFileName,2);
C_data_ArrayMean(:,:,idx)=mean(C_data_Array(:,:,idx),2);
C_data_ArrayStd(:,:,idx)=std(C_data_ArrayMean(:,:,idx));
LEDdetectThresh=(mean(C_data_ArrayMean(:,:,idx),1))+C_data_ArrayStd(:,:,idx);
LEDdurIndx=find(C_data_ArrayMean(:,:,idx)>LEDdetectThresh);
LED_duration_=LEDdurIndx([1,end]);
LEDinit(idx)=LED_duration(1);
LEDend(idx)=LED_duration(2);
BR = Binarizing_Rowsum(:,idx);
BR(LEDdurIdx) = Light;
Binarizing_Rowsum(:, idx) = BR;
end
Inside your loop, you are assigning a whole new value to LEDdurIndx. This makes the variable a parfor "temporary", and so any value assigned inside the loop is discarded. I don't see any such problem for Binarizing_Rowsum though...
@Edric EllisThank you for the comment. Can you help me understand then how do I assign this temporary value of LEDduration to an array?
In this case I'm assigning the value from the temporary LEDdurIndx to LED_duration, and then I'm allocating that to both LEDinit(idx) and LEDend(idx) which I want to output outside the loop. But I don't get the output I expect. What is the most proper way to allocate the value LED_duration and LEDdurIdx outside of the parfor loop?

请先登录,再进行评论。

LEDdetectThresh=(mean(C_data_ArrayMean(:,:,idx),1))+C_data_ArrayStd(:,:,idx);
LEDdurIndx=find(C_data_ArrayMean(:,:,idx)>LEDdetectThresh)
I suspect the code is buggy, the mean can never be bigger than mean + std
LEDdurIndx is therefore empty.

1 个评论

Thanks for the comment. The mean in the second line is in the second dimension, however the one in the first line is in the first dimension. It shouldn't be a problem, the code is working perfectly in regular for-loop, but now I'm trying to convert it to parfor loop and that s why I'm facing these problems.

请先登录,再进行评论。

Your code is good to be called "spagetti". If all the image have the same size, you must tell parfor what they are:
jValues=1:2:length(baseFileNames)-1;
n = numel(jValues);
Binarizing_Rowsum=zeros(6100,n);
LEDdurIndx=NaN(1000,n);
Light=3000;
j=jValues(1);
%Read first file to figure out the array sizes
fullFileName=fullfile(thisFolder, baseFileNames{j});
I1= imread(fullFileName,2);
[p,q]=size(I1);
C_data_Array = zeros(p,q,n);
C_data_ArrayMean = zeros(p,1,n);
C_data_ArrayStd = zeros(1,q,n);
LEDdetectThresh = zeros(1,q,n);
Binarizing_Rowsum = zeros(p*q,n);
parfor idx=1:n
j=jValues(idx);
%Read files
fullFileName=fullfile(thisFolder, baseFileNames{j});
C_data_Array(:,:,idx)= imread(fullFileName,2);
C_data_ArrayMean(:,:,idx)=mean(C_data_Array(:,:,idx),2);
C_data_ArrayStd(:,:,idx)=std(C_data_ArrayMean(:,:,idx));
LEDdetectThresh=(mean(C_data_ArrayMean(:,:,idx),1))+C_data_ArrayStd(:,:,idx);
LEDdurIndx = find(C_data_ArrayMean(:,:,idx)>LEDdetectThresh);
LED_duration_=LEDdurIndx([1,end]); %Updated
LEDinit(idx)=LED_duration(1);%Updated
LEDend(idx)=LED_duration(2);%Updated
Temp = zeros(p*q,1);
Temp(LEDdurIndx) = Light;
Binarizing_Rowsum(:,idx)=Temp;
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by