【问题标题】:MATLAB App Designer reset propertiesMATLAB App Designer 重置属性
【发布时间】:2018-05-10 11:04:38
【问题描述】:

我在 MATLAB App Designer 中定义了一个私有属性列表,它们的初始化如下:

properties (Access = private)
    prop1 = val1;
    prop2 = val2;
    ...
end

我现在想要一个函数将它们重置为上面定义的默认值。有没有办法自动执行此操作,或者我必须手动重置它们(这可能导致错误,例如添加更多属性时)?

另外,有没有办法循环遍历我以这种方式定义的所有属性?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    如果您想全面重置私有属性,您可以使用metaclass 访问您的属性的属性并根据需要进行调整。

    例如:

    classdef SOcode < handle
        properties
            a
            b
        end
    
        properties (Access = private)
            c = -1
            d = -1
        end
    
        methods
            function self = SOcode()
            end
    
            function changeprivate(self)
                self.c = randi(5);
                self.d = randi(5);
            end
    
            function printprivate(self)
                fprintf('c = %d\nd = %d\n', self.c, self.d)
            end
    
            function resetprivate(self)
                tmp = metaclass(self);
                props = tmp.PropertyList;
                nprops = numel(props);
    
                for ii = 1:nprops
                    if strcmp(props(ii).SetAccess, 'private')
                        self.(props(ii).Name) = props(ii).DefaultValue;
                    end
                end
            end
        end
    end
    

    提供所需的行为:

    >> test = SOcode;
    >> test.changeprivate;
    >> test.printprivate;
    c = 1
    d = 1
    >> test.resetprivate;
    >> test.printprivate;
    c = -1
    d = -1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-17
      • 2018-08-02
      • 1970-01-01
      • 2021-02-14
      • 2021-06-14
      • 2020-10-09
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多