Azzera filtri
Azzera filtri

Variable handling problem with parfor

1 visualizzazione (ultimi 30 giorni)
AR
AR il 4 Ago 2023
Commentato: AR il 6 Ago 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.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by