2013-03-08 88 views
2

我想定製一些MATLAB的uicontrols(如下拉框),給他們更多的用戶友好的功能。擴展MATLAB uicontrol

我的問題是:是否可以擴展/繼承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 

回答

0
  1. 沒有記錄的方式(如R2013a的)寫子類,以MATLAB句柄圖形 類。事實上,由於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 
    

    然後,您可以使用ComboBox,就像任何其他uicontrol手柄:

    obj = ComboBox(figure, 'hello|goodbye'); 
    set(obj, 'items', 'newItem1|newItem2'); 
    
  3. 有延伸的把手圖形類, 無證方式,但我不熟悉它們。查看該參考文獻: http://undocumentedmatlab.com/blog/introduction-to-udd/