Matlab GUI代码后面的全局(共享)变量.有没有更好的方法,然后使用句柄结构呢?

问题描述:

我最近花了一些时间来创建复杂的Matlab GUI工具.我正在使用GUIDE GUI Builder.

I have spent some time lately creating complex Matlab GUI tool. I'm using GUIDE GUI builder.

我不喜欢Matlab在后面的代码(带有函数的.m文件)中管理全局变量的方式.如果要在函数调用之间共享数据,则必须将它们存储在handles结构中,该结构作为每个函数调用的参数提供.我认为它易于出错,因为我必须时刻注意存储和更新handles.以这种方式处理全局变量对我来说是很不自然的.使用 appdata 更加糟糕.

I dislike the way Matlab manages global variables in code behind (the .m file with functions). If I want to share data between function calls I have to store them in handles structure that is given as a parameter to every function call. I consider it to be bug prone as I have to mind storing and updating handles all the time. It is rather unnatural for me to handle global variables this way. Using appdata is even worse.

是否可以将类用作GUI的代码?所有的回调都是方法,因此它们将接收obj作为参数,并且我可以将全局变量存储在类属性中.在我看来这要清楚得多.

Is it possible to use class as a code behind for GUI? All the callbacks would be methods so they would receive obj as a parameter and I could store global variables in class properties. This seems to me to to be much clearer.

这只是一个想法,可能根本不可能...但是,如果有更好的做法,如何在Matlab GUI中处理全局变量,请提出建议.

It is just an idea and it may be not possible at all... But if there is better practise how to deal with global variables in Matlab GUI then please suggest it.