Clear all workspace except variables use in GUI

1 visualización (últimos 30 días)
Mathieu
Mathieu el 19 de Jul. de 2016
Editada: Stephen23 el 20 de Jul. de 2016
Hello,
I am here today because I am developing a GUI in which I use some global variables, and handles of objects of the GUI. One of my button launches a script in which I would like to clear all variable which could be related to this script, but without touching the others variables that MATLAB is using for the GUI (and the global variables I instanciated).
Do you think it is possible?
I thank you in advance for your help. Best regards.
  2 comentarios
Stephen23
Stephen23 el 19 de Jul. de 2016
Editada: Stephen23 el 20 de Jul. de 2016
Mixing globals, scripts, and a GUI together is a recipe for buggy, hard-to-maintain code.
Globals are a not a good way to pass data between workspaces, because they are very difficult to keep track of when they change, making bugs likely and debugging very hard. Scripts are much loved by beginners, but any serious code need to use the independent workspaces that functions offer.
Why not take the opportunity to learn how to program robustly, e.g. by passing data between workspaces using one of the recommended methods (not globals!):
As Doug says "I have never seen MATLAB code where globals were the right thing to do":
"Beginners dive right in to globals because they think using them is easy. They are also easy to do wrong. ... I always end up teaching people to program by careful data management rather than relying on globals."
And scripts really should't be used for anything that will be used for more than ten minutes:
Mathieu
Mathieu el 19 de Jul. de 2016
I will do this if I have the time but for my current project, it is to late I think. For next one, for sure I won't use global variables !
But thanks for all these links ;)

Iniciar sesión para comentar.

Respuesta aceptada

Adam
Adam el 19 de Jul. de 2016
Editada: Adam el 19 de Jul. de 2016
Why don't you just use a function instead of a script? GUIs and scripts are not a great mix in general.
Also, this is one of many reasons not to use global variables. If you use functions rather than scripts and global variables then everything has its own workspace and you pass around the varibles you want shared between workspaces, if need be wrapping them in a struct if you are passing a large number of variables (though if you are passing a large number of variables to a function there is probably something wrong in the design).
Variables remain within the scope of a function workspace, but are automatically cleared when the function scope ends.
  2 comentarios
Mathieu
Mathieu el 19 de Jul. de 2016
I can't modify my GUI now and replace global variables. Too much work to do. But I can speak with my colleague about his script (that I have to include in my GUI) and see if I can transform it in a function which can solve the problem like you said. Thanks for your answer.
Stephen23
Stephen23 el 19 de Jul. de 2016
"Too much work to do"
fixing all the global-bugs will be a lot of work too.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function Creation 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