MATLAB: Clear all workspace except variables use in GUI

clear variableMATLABmatlab gui

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.

Best Answer

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.