执行属性get方法时执行的构造函数

时间:2017-01-01 06:25:53

标签: matlab oop inheritance

我希望继承内置控件,例如axes;但是,据我所知,MATLAB不会以文件化的方式允许这样做。为了解决这个问题,我创建了一个名为MyAxes的类,如下所示。

MyAxes有一个名为MATLABAxes的已定义属性,它存储matlab.graphics.axis.Axes个对象。此轴对象在构造时创建。每个轴属性都动态添加到正在构造的MyAxes对象中,创建应该简单地重定向到MATLABAxes属性的包装器属性。

每个包装器属性的get方法设置为MyAxes方法get_axes_property。此方法有三个参数:

  1. MyAxes对象本身
  2. 对轴控件的引用
  3. 属性名称
  4. 除了为每个属性创建一个新轴外,此方法效果很好。我最终得到一个有131个子轴的图!由于在MyAxes构造函数中创建了轴,这似乎正在发生。解决此问题的方法是要求首先创建MATLAB轴并将其作为参数传递给构造函数。这很不方便。

    如果不创建多个轴,如何在MyAxes构造函数中维护轴创建?当然,如果我偏离轨道并且有更好的方法来对内置控件进行分类,我很乐意听到它。

    classdef Axes < handle & dynamicprops
    
        properties
            MATLABAxes;
        end
    
        methods
    
            function obj = Axes
                obj.MATLABAxes = axes;
                axesPropertyList = properties( obj.MATLABAxes );
                for property = axesPropertyList(:)'
                    propertyName = property{1};
                    obj.addprop( propertyName );
                    propertyInstance = obj.findprop( propertyName );
                    propertyInstance.GetMethod = @(x,y)obj.get_axes_property( obj.MATLABAxes, propertyName );
                end
            end
    
            function value = get_axes_property( obj, control, propertyName )
                value = control.(propertyName);
            end
    
        end
    
    end
    

1 个答案:

答案 0 :(得分:0)

再次 - 在写这个问题时回答了我自己的问题。这是一个拼写错误。我已将函数句柄定义中get_axes_property方法的第一个参数放在axes而不是obj.MATLABAxes。每次获取属性时执行axes函数。问题中的代码现已修复。