How to resolve "The variable in a parfor cannot be classified" error?
조회 수: 1 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (1개)
Walter Roberson
2018년 6월 13일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!