How to resolve "The variable in a parfor cannot be classified" error?

1 view (last 30 days)
When I try to run the following code:
parfor n = 1:size(chan_combos_idx,1)
for pre_same_z = 1:size(pre_same_snip_all,2)
pre_same_cpsd(:,pre_same_z,n) = cpsd(pre_same_snip_all(:,pre_same_z,chan_combos_idx(n,1)),pre_same_snip_all(:,pre_same_z,chan_combos_idx(n,2)));
end
for pre_diff_z = 1:size(pre_diff_snip_all,2)
pre_diff_cpsd(:,pre_diff_z,n) = cpsd(pre_diff_snip_all(:,pre_diff_z,chan_combos_idx(n,1)),pre_diff_snip_all(:,pre_diff_z,chan_combos_idx(n,2)));
end
for post_same_z = 1:size(post_same_snip_all,2)
post_same_cpsd(:,post_same_z,n) = cpsd(post_same_snip_all(:,post_same_z,chan_combos_idx(n,1)),post_same_snip_all(:,post_same_z,chan_combos_idx(n,2)));
end
for post_diff_z = 1:size(post_diff_snip_all,2)
post_diff_cpsd(:,post_diff_z,n) = cpsd(post_diff_snip_all(:,post_diff_z,chan_combos_idx(n,1)),post_diff_snip_all(:,post_diff_z,chan_combos_idx(n,2)));
end
end
I get the error "The variable pre_same_cpsd in a parfor cannot be classified. I think this has something to do with this being a sliced variable, but I'm not sure how to resolve it.
Thanks in advance!

Answers (1)

Walter Roberson
Walter Roberson on 13 Jun 2018
Inside the parfor loops, you should write to temporary variables like local_same_pre_cpsd(:,pre_samez) with no third subscript. Once you have completed the local variable for loop, write the entire variable into same_pre_cpsd(:,:,n) as one assignment statement. Use the same kind of technique for the other output variables.
You might also need to extract pre_same_snip_all(:,pre_same_z,:) into a local variable, and index that local variable at chan_combos_idx(n,2) and pass that to your function.
In summary, if you have an input variable being accessed with a parfor variable, then you should copy out the entire slice into a local variable and access the slice, and if you have an output variable being accessed with a parfor variable then create a local variable which is a slice, and then afterwards copy the slice to the output variable all at one time.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!