How to track new variables in the workspace

1 visualización (últimos 30 días)
Nicholas Ayres
Nicholas Ayres el 4 de Jul. de 2017
Comentada: Stephen23 el 4 de Jul. de 2017
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 comentario
Stephen23
Stephen23 el 4 de Jul. de 2017
Editada: Stephen23 el 4 de Jul. de 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?

Iniciar sesión para comentar.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 4 de Jul. de 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 comentarios
Nicholas Ayres
Nicholas Ayres el 4 de Jul. de 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 el 4 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by