Needing 'clear all' type function, except a few variables. clearvars -except isn't aggressive enough
5 views (last 30 days)
Show older comments
I want to use clear all, except keep certain variables.
I am using a third party toolkit, and basically want to clear out the buffer and other variables being used in that toolkit. I have no other way to clear it, so as I make requests, the requests take longer and longer until the program basically stops running. This happens pretty quickly as I'm making a lot of requests.
'clearvars -except var_name' doesn't have the same behavior as 'clear all', as it's not aggressive enough to take out everything, so it doesn't work for my application.
Right now I'm doing this:
- save the relevant variables to file
- clear all
- load the relevant varaibles back from file
This makes the code take forever to run since it's saving/loading a lot of data.
Does anyone know of a way around this? I'm thinking there must be some way, but I'm not finding anything in searches.
0 Comments
Answers (1)
Steven Lord
on 1 Sep 2023
From your description I'm guessing the third party toolkit is written using a lot of script files rather than functions or classes. If so I recommend writing functions that wrap those script files. Have those functions only accept as inputs the data those scripts specifically and explicitly require for their operation and only return the outputs that you absolutely need from those scripts. Let the intermediate "helper" variables those scripts create stay in the function workspace and get destroyed automatically when the function calls finish executing and their workspaces get cleaned up normally.
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!