필터 지우기
필터 지우기

Variable handling problem with parfor

조회 수: 1 (최근 30일)
AR
AR 2023년 8월 4일
댓글: AR 2023년 8월 6일
I had some code that involved a for loop and variables, approximately thus:
if ~isempty(iList)
d = iList(k);
end
parfor s = 1:maxS
% various lines of code
if exist('d', 'var')
iString = num2str(d);
else
iString = 'blah-blah';
end
% various other lines of code
end
if exist('d', 'var')
iLegend = cellstr(num2str(iList'));
else
iLegend = 'blah-de-blah';
end
iList is set to empty [] when the function that contains this code is called. So the variable d doesn't exist anywhere in the code.
Before I converted this code to run with parfor, the regular for loop and the whole function/script ran without errors. Now, it errors-out and reports: "Unrecognized function or variable 'd'."
I don't understand why there is a problem. Since I can't debug the parfor parallelization, I don't have any more specifics - does the error come from within the loop or outside it, etc? Please help me identify. I am unaccustomed to working with parallelization and have been having some trouble with uninitialized temporaries, sliced structures, temporary variables that are not initialized outside the loop, not used after the loop, etc. I did manage to modify the code enough that no static errors were reported, but obviously there is still some runtime error.

채택된 답변

Matt J
Matt J 2023년 8월 4일
편집: Matt J 2023년 8월 4일
I would try as below. This will be faster than your original version anyway, even before the conversion from for to parfor.
if ~isempty(iList)
d = iList(k);
else
d=[];
end
parfor s = 1:maxS
% various lines of code
if ~isempty(d)
iString = num2str(d);
else
iString = 'blah-blah';
end
% various other lines of code
end
  댓글 수: 1
AR
AR 2023년 8월 6일
Thanks - that worked. I don't know why the non-existence of the variable is a problem, though. Regardless, the problem is solved.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by