How to track new variables in the workspace

1 view (last 30 days)
I am running script functions (of which I have no way of knowing all of their contents or variables) in the same workspace as my own variables. I know, and will be testing, some of the outputs from the script.
What I need to be able to do is remove all variables created by the script without removing my own. I could use
clearvars -except my_vars
in order to preserve my own variables. But it would be extremely helpful to the runtime of my code to be able to remove the created variables specifically as I may need to run a couple of different scripts before wanting to remove variables from only one of them.
---
Edit: Have worked out an alternative method to solving my problem, thanks to Geoff Hayes.
This question is now out of curiosity rather than practicality as to whether I can manipulate the workspace in this manner.
  1 Comment
Stephen23
Stephen23 on 4 Jul 2017
Edited: Stephen23 on 4 Jul 2017
Or, instead of writing slow and buggy code that relies on introspection and clearvars, you could simply convert all of your scripts into functions. Bingo, problem solved!
Functions do not suffer the disadvantages of workspace pollution like scripts do. Although beginners love writing scripts for everything scripts really should not be used for anything important, anything whose output must be deterministic, or for anything that will be needed for more than one day. Functions are much preferred for any real work, and functions are what experienced users would use.
Automagically detecting and clearing variables is going to be slow, make debugging difficult, and obfuscate the code. Why bother?

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 4 Jul 2017
Nicholas - try using functions instead of scripts (see scripts vs functions for a description of the differences between the two). Functions have their own workspace and so won't interfere/pollute the base workspace with them. If you need your functions to have access to the base workspace variables, then consider passing them in as input parameters to your functions.
  2 Comments
Nicholas Ayres
Nicholas Ayres on 4 Jul 2017
Unfortunately I can't convert all the scripts to functions (as I am not the one who wrote them, hence the lack of knowledge about their contents), but I could try to run them in a sub-function, only passing in the crucial information.
Thank you.
Although I would still like to know if there is a way to manipulate the workspace in such a manner. Now just for curiosity, rather than practicality.
Stephen23
Stephen23 on 4 Jul 2017
"but I could try to run them in a sub-function, only passing in the crucial information."
Writing wrappers is a good idea.
"I would still like to know if there is a way to manipulate the workspace in such a manner"
Probably using whos and clearvars. It would be slow and ugly though.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!