Matlab 将类属性转换为关于可见性的结构

Matlab 将类属性转换为关于可见性的结构,matlab,oop,struct,class-visibility,Matlab,Oop,Struct,Class Visibility,我正在开发一个simulink模型,该模型配置在不同的模式下,根据所选采样率、滤波器组延迟等更改模型参数 我想在ParameterStruct上设置所有参数,然后为每个模式加载适当的参数结构 这类映射很好地映射到一个具有依赖属性的类,因为有很多模型参数仅由几个输入生成 但是当我试图从a类生成结构时,可见性没有得到尊重: classdef SquareArea properties Width Height end properties (Access =

我正在开发一个simulink模型,该模型配置在不同的模式下,根据所选采样率、滤波器组延迟等更改模型参数

我想在
ParameterStruct
上设置所有参数,然后为每个模式加载适当的参数结构

这类映射很好地映射到一个具有依赖属性的类,因为有很多模型参数仅由几个输入生成

但是当我试图从a
类生成
结构时,可见性没有得到尊重:

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

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

您可以覆盖类的默认结构:

publicProperties = properties(x);
myStruct = struct();
for iField = 1:numel(publicProperties), myStruct.(publicProperties{iField}) = []; end
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
现在:

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

>> 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

您可以覆盖类的默认
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
现在:

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

>> 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

结合Amro和DVarga的答案,可以推广默认结构函数:

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

结合Amro和DVarga的答案,可以推广默认结构函数:

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