【问题标题】:Converting class properties to struct respecting visibility将类属性转换为尊重可见性的结构
【发布时间】:2016-07-30 21:49:17
【问题描述】:

我正在研究一个 simulink 模型,该模型配置在不同的模式上,这些模式会根据所选采样率滤波器组延迟等内容更改模型参数...

我想在ParameterStruct 上设置所有参数,然后为每种模式加载正确的参数结构。

这种类型可以很好地映射到具有 Dependent 属性的类,因为有很多模型参数仅由几个输入生成。

但是,当我尝试从 class 生成 struct 时,不尊重可见性:

classdef SquareArea
   properties
      Width
      Height
   end
   properties (Access =private)
      Hidden
   end
   properties (Dependent)
      Area
   end
   methods
      function a = get.Area(obj)
         a = obj.Width * obj.Height;
      end
   end
end
>> x=SquareArea

x = 

  SquareArea with properties:

     Width: []
    Height: []
      Area: []

>> struct(x)
Warning: Calling STRUCT on an object prevents the object 
from hiding its implementation details and should thus
be avoided. Use DISP or DISPLAY to see the visible public
details of an object. See 'help struct' for more information.

ans = 

     Width: []
    Height: []
    Hidden: []
      Area: []

这是不可接受的,因为之后我需要将结构导出到 C,以便能够从生成的代码中动态设置模式。

【问题讨论】:

    标签: matlab oop struct class-visibility


    【解决方案1】:

    您可以为您的班级覆盖默认的struct

    classdef SquareArea
       properties
          Width = 0
          Height = 0
       end
       properties (Access=private)
          Hidden
       end
       properties (Dependent)
          Area
       end
       methods
          function a = get.Area(obj)
             a = obj.Width * obj.Height;
          end
          function s = struct(obj)
              s = struct('Width',obj.Width, 'Height',obj.Height, 'Area',obj.Area);
          end
       end
    end
    

    现在:

    >> obj = SquareArea
    obj = 
      SquareArea with properties:
    
         Width: 0
        Height: 0
          Area: 0
    >> struct(obj)
    ans = 
         Width: 0
        Height: 0
          Area: 0
    

    请注意,您仍然可以通过显式调用内置函数来获得原始行为:

    >> builtin('struct', obj)
    Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should
    thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for
    more information. 
    ans = 
         Width: 0
        Height: 0
        Hidden: []
          Area: 0
    

    【讨论】:

      【解决方案2】:
      publicProperties = properties(x);
      myStruct = struct();
      for iField = 1:numel(publicProperties), myStruct.(publicProperties{iField}) = []; end
      

      【讨论】:

        【解决方案3】:

        结合 Amro 和 DVarga 的答案,可以概括出一个默认的结构函数:

             function s = struct(self)
                publicProperties = properties(self);
                s = struct();
                for fi = 1:numel(publicProperties)
                    s.(publicProperties{fi}) = self.(publicProperties{fi}); 
                end                  
            end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-05
          • 2023-03-09
          • 2011-03-02
          • 2020-12-01
          • 2020-02-07
          相关资源
          最近更新 更多