Filter löschen
Filter löschen

Variable handling problem with parfor

1 Ansicht (letzte 30 Tage)
AR
AR am 4 Aug. 2023
Kommentiert: AR am 6 Aug. 2023
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.

Akzeptierte Antwort

Matt J
Matt J am 4 Aug. 2023
Bearbeitet: Matt J am 4 Aug. 2023
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 Kommentar
AR
AR am 6 Aug. 2023
Thanks - that worked. I don't know why the non-existence of the variable is a problem, though. Regardless, the problem is solved.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by