How to clear some variables or set them to '[ ]' when inside parfor loop?

20 views (last 30 days)
When using it doesn't let you clear variables inside due to "Transparency Violation Error". One suggestion I found was to set the variables I want o [ ] instead of clear -except var1, var2... But I'd like to evidently set them all at once, because I'm only interested in keeping a few, the rest should be cleared/set to [ ].
Any ideas?
Thanks!
  1 Comment
Calin O
Calin O on 20 May 2014
Consider I have many variables and I just want to keep a few, and the rest to clear/erase/delete/set them to "[ ]".

Sign in to comment.

Answers (1)

Matt J
Matt J on 20 May 2014
Edited: Matt J on 20 May 2014
If you hold the variables in struct fields, you can do, for example,
parfor i=1:10
S.a=1;
S.b=2;
S.c=3;
S.d=4;
a=S.a; b=S.b; %keep these
S=structfun(@(f)[] ,S,'uni',0);
end
  2 Comments
Calin O
Calin O on 21 May 2014
the variables(known before the parfor-loop) are not in any struct, just wanted to start the loop, clean a few, and then continue with the statements. I don't want to store all of the variables created until that point in a struct. Something like that:
parfor numsim = 1:20
clear -except var1, var2 %keep these variables
<statements>
end
but instead of the 2nd line, to have smth else, because of that "transparency error".
If you can think of any way, let me know. Thanks.
Matt J
Matt J on 21 May 2014
Edited: Matt J on 21 May 2014
the variables(known before the parfor-loop) are not in any struct
If you define a variable prior to a parfor loop and then set it to [] within the loop, it will have the value [] within the loop. When the loop ends, however, it will return to its initial value. I.e., only copies broadcast to the parallel workers actually get changed, not the original copy on the host.
I don't want to store all of the variables created until that point in a struct.
I don't think you have any other option if you want a 1-line method of setting them all to empty.

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!