Clear all workspace except variables use in GUI
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2016 年 7 月 19 日
編集済み: Stephen23
2016 年 7 月 20 日
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:
採用された回答
Adam
2016 年 7 月 19 日
編集済み: Adam
2016 年 7 月 19 日
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 件のコメント
Stephen23
2016 年 7 月 19 日
"Too much work to do"
fixing all the global-bugs will be a lot of work too.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!