【问题标题】:Extending MATLAB uicontrol扩展 MATLAB uicontrol
【发布时间】:2013-02-24 06:49:49
【问题描述】:

我想自定义一些 MATLAB uicontrols(例如下拉框),以赋予它们更多用户友好的功能。

我的问题是:是否可以扩展/继承 uicontrol?如果是这样,你怎么做?如果没有,是否有解决方法?

我已尝试使用此基本代码进行设置,但收到以下错误:

The specified super-class 'uicontrol' contains a parse error or cannot be found on MATLAB's search path, possibly shadowed by another file with the same name.

classdef ComboBox < uicontrol    
    methods(Access = public)
        function obj = ComboBox()
            set(obj, 'Style', 'popup');
        end 
    end
end

当我尝试将其添加到图形时发生错误:

cmb = ComboBox();
set(cmb, 'Parent', obj.ui_figure);

编辑:经过考虑,我认为这将是一个不错的解决方法,但是,如果可能的话,我仍然想知道如何扩展 uicontrol。

classdef ComboBox < uicontrol    
    properties(Access = public)
       Control;
    end

    methods(Access = public)
        function obj = ComboBox(parent, items)
            obj.Control = uicontrol();
            set(obj.Control, 'Style', 'popup');
            set(obj.Control, 'Parent', parent);
            set(obj.Control, 'String', items);
        end 
    end
end

【问题讨论】:

    标签: oop matlab user-interface


    【解决方案1】:
    1. 没有记录的方式(从 R2013a 开始)将子类写入 MATLAB Handle Graphics 类。事实上,由于 MATLAB 存储 hg 对象的方式,我们甚至无法获取 hg 对象的类很容易。例如:

      >> fig = figure();
      >> class(f)
      
      ans =
      
      double
      
      >> isa(f, 'figure')
      
      ans =
      
           0
      
      >> ishghandle(f)
      
      ans =
      
           1
      
    2. 对此的一种解决方案是编写一个子类handle
      hgsetget 的类,并将 uicontrol 对象的句柄保留为私有属性。 例如:

      classdef ComboBox < hgsetget
          properties (Access = private)
              Control
          end
      
          properties
              % extend the functionality of your new uicontrol with additional
              % properties
              newProp1
              newProp2
          end
      
          properties (Dependent = true)
              % make any properties of uicontrol for which you want to still
              % allow access as dependent properties. define set and get methods
              % for these properties below
              fontSize
              foregroundColor
              backgroundColor
              items
          end
      
          methods
              function obj = ComboBox(parent, items)
                  obj.Control = uicontrol(parent, 'Style', 'popup', ...
                      'String', items);
                  % make sure to delete this object if you delete the uicontrol
                  set(obj.Control, 'DeleteFcn', {@(source, eventData)delete(obj)})
              end
      
              % Define set and get methods for your new properties. These methods
              % will set the actual properties, and then modify the uicontrol in
              % some way
              function prop = get.newProp1(obj)
                  prop = obj.newProp1;
              end
      
              function set.newProp1(obj, newVal)
                  obj.newProp1 = newVal;
                  % do something else
              end
      
              function prop = get.newProp2(obj)
                  prop = obj.newProp2;
              end
      
              function set.newProp2(obj, newVal)
                  obj.newProp2 = newVal;
                  % do something else
              end
      
              % Define set and get methods for any uicontrol properties you wish
              % to retain. These methods will simply redirect calls to the
              % uicontrol object.
              function size = get.fontSize(obj)
                  size = get(obj.Control, 'FontSize');
              end
      
              function set.fontSize(obj, newSize)
                  set(obj.Control, 'FontSize', newSize);
              end
      
              function color = get.backgroundColor(obj)
                  color = get(obj.Control, 'BackgroundColor');
              end
      
              function set.backgroundColor(obj, newColor)
                  set(obj.Control, 'BackgroundColor', newColor);
              end
      
              function color = get.foregroundColor(obj)
                  color = get(obj.Control, 'ForegroundColor');
              end
      
              function set.foregroundColor(obj, newColor)
                  set(obj.Control, 'ForegroundColor', newColor);
              end
      
              % You can even rename some uicontrol properties to fit your
              % purpose.
              function items = get.items(obj)
                  items = get(obj.Control, 'String');
              end
      
              function set.items(obj, newItems)
                  set(obj.Control, 'String', newItems);
              end
      
          end
      end
      

      然后您可以像使用任何其他uicontrol 句柄一样使用ComboBox

      obj = ComboBox(figure, 'hello|goodbye');
      set(obj, 'items', 'newItem1|newItem2');
      
    3. 存在扩展句柄图形类的未记录方法, 但我不熟悉他们。看看这个参考: http://undocumentedmatlab.com/blog/introduction-to-udd/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      相关资源
      最近更新 更多