将GUIDE与面向对象的MATLAB一起使用? [英] Using GUIDE with object-oriented MATLAB?

查看:51
本文介绍了将GUIDE与面向对象的MATLAB一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要GUI的面向对象的MATLAB应用,并且我想至少使用GUIDE作为布局.我已经尝试了手动方式,但是进行控件定位实在是太痛苦了.

I have an object-oriented MATLAB app that needs a GUI, and I'd like to use GUIDE for the layout (at least). I've tried the manual way, and doing the control positioning is just too painful.

我已经注意到GUIDE非常面向过程.它会生成M文件,这些文件假定它们是从路径运行的,并且未与任何类或对象相关联.

I've noticed that GUIDE is very much procedurally-oriented; it generates M-files that assume they are run from the path and aren't associated with any classes or objects.

任何人都有尝试以面向对象的方式使用GUIDE的经验吗?如果简单明了,我也想自动生成代码,但是我愿意让GUIDE生成.fig文件并自己编写代码.

Has anyone had experience trying to use GUIDE in an object-oriented way? If it's straightforward, I'd like to do automatic code generation as well, but I'm willing to let GUIDE just generate the .fig file and write the code myself.

推荐答案

在创建带有指南的gui时,对于放置在窗格上的每个按钮/文本框/图形等,它会自动生成用于必要回调的外壳,因此,您要做的就是填写代码.如果您更改小部件的名称(它们的标签")或添加或删除它们,它将为您更新m文件,这很方便.

When you create a gui with guide, for every button/textbox/graph etc. you put on the pane, it automatically generates the shells for the necessary callbacks, so all you have to do is fill in the code. If you change the name of the widgets (their "tags") or add or delete them, it updates your m-file for you, which is handy.

可以将您的gui与对象相关联;自动生成的m文件的功能轮廓如下所示:

You can associate your gui with objects; the autogenerated m-file has a function outline that looks like this

function YourGUIName_OpeningFcn(hObject, eventdata, handles, varargin)

您可以要求某人通过varargin向您的gui传递一个或多个对象. Matlab的规范方法是传递参数名称/值对,因此从命令行进行的调用看起来像

you can require that someone pass your gui an object or objects through the varargin. The canonical matlab way to do this is to pass parameter name/value pairs, so the call from the command line would look like

YourGuiName('importantobject', object1);

但是您也可以(特别是如果只有一个唯一的参数)假定varargin {1}是一个特定参数,varargin {2}是第二个参数,依此类推

but you could also (especially if there is just one unique argument) assume varargin{1} is a specific parameter, varargin{2} is a second, and so on

在这种情况下,从命令行的调用将是

In this case, the call from the command line would be

YourGuiName(object1);

然后在您的openingfcn中添加一行

In your openingfcn, you would then add a line like

if (length(varargin) < 1) || ~isa(varargin{1}, 'importantObjectType')
     error ('you must pass an importantobject to YourGuiName, see help');
end
myimportantobject = varargin{1}

您现在可以选择.规范地将数据存储在gui中的正确方法是将其放入handles结构中,然后与guidata一起存储,如

You now have a choice to make. The canonically correct way to store data in your gui is to put it in the handles structure and then store it with guidata, as in

handles.myobject = varargin{1};
guidata(hObject, handles); %this is just boilerplate 

样板是必需的,因为尽管其名称是handles的子类,但它不是Handle的子类,而是通过值(而不是引用)传递的. guidata命令会在与gui图形关联的某个地方粘贴句柄,以便您可以在后续的回调中获取它.

The boilerplate is necessary because, despite its name, handles does not subclass Handle, and is passed by value, not reference. the guidata command sticks handles somewhere associated with the gui figure so you can get it in subsequent callbacks.

这种方法的问题是,当您将大对象放在句柄中时,它会使guidata命令永久占用.即使MATLAB除非绝对必要,也不应该在按值传递数据时复制数据,但事实是这样,如果您的对象是Handle,则需要4个字节来回传递,这甚至是正确的.不要问我为什么,但是我怀疑这与内存管理和功能有关.垃圾收集.

The problem with this approach is that when you put a large object in handles, it makes the guidata command take forever. This is true even though MATLAB is not supposed to copy data when passing by value unless absolutely necessary, and it is even true if your object is a Handle, which takes like 4 bytes to pass back and forth. Don't ask me why, but I suspect it has something to do with memory management & garbage collection.

如果您的gui需要一段时间来执行命令,并且您使用profile并看到它挂在guidata命令上,则只需声明您的对象为全局对象并以这种方式处理它即可.

If your gui is taking a while to execute commands, and you use profile and see it hanging on the guidata command, you should just declare your object to be a global and deal with it that way

global YOURGUI_object;  %it's not my fault; blame MATLAB
YOURGUI_object = varargin{1};

然后,您可以让所有回调执行所需的YOURGUI_object的任何方法.

Then you can just have all your callbacks execute whatever method of YOURGUI_object they need.

祝你好运.

这篇关于将GUIDE与面向对象的MATLAB一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆