Consider I have many variables and I just want to keep a few, and the rest to clear/erase/delete/set them to "[ ]".
How to clear some variables or set them to '[ ]' when inside parfor loop?
21 views (last 30 days)
Show older comments
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!
Answers (1)
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
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.
See Also
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!