Error: The variable Agent in a parfor cannot be classified

1 view (last 30 days)
parfor ii = 1:Workspace.Number_of_Compartments
Comp(ii).Vehicles = zeros(length(Compartment(ii).Vehicles),2)
for jj = 1:length(Compartment(ii).Vehicles)
Veh_ID = Compartment(ii).Vehicles(jj);
for zz = 1:length(Agent(Veh_ID).Structure.Following_Compartment)
if (Agent(Veh_ID).Structure.Following_Compartment(zz) == ...
Agent(Veh_ID).Structure.Local_Task = [Agent(Veh_ID).Structure.Local_Task,...
cur_comp = Agent(Veh_ID).Structure.Current_Compartment;
fol_comp = Agent(Veh_ID).Structure.Following_Compartment(zz);
Find1 = find(Compartment(cur_comp).Following_Compartment == fol_comp);
% All elements of the Find1 row of the field Exite_Nodes of
% the Compartment(cur_comp)
Find2 = Compartment(cur_comp).Exite_Nodes(Find1,:);
Find2_1 = find(Find2 ~= 0);
Find3 = find(Compartment(cur_comp).Exite_Nodes_State(Find1,:) == 0);
Find4 = intersect(Find2_1,Find3);
Find4 = Find4(randi(length(Find4)));
Agent(Veh_ID).Structure.Local_Task = [Agent(Veh_ID).Structure.Local_Task,...
How to eliminate this error? I need to add data in the structure inside parfor loop.

Answers (1)

Jemima Pulipati
Jemima Pulipati on 22 Dec 2020
From my understanding, you are using the 'Agent' variable for slicing as well as indexing and trying to assign a value to it. This error arises because once you index a variable you cannot use it anywhere else in the parfor loop.
The suggested workaround would be to use a temporary variable inside the for loops and one 'Agent' variable that gets assigned the required values in the main parfor loop. In this way the variable 'Agent' would be referenced only once in the entire parfor loop.
Some answers from the community which might be of relevance to you are:

Community Treasure Hunt

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

Start Hunting!