我正在开发一个在不同模式上配置的simulink模型,它根据所选的采样率过滤器组延迟等内容更改模型参数...
我虽然在ParameterStruct
上设置了所有参数,但为每种模式加载了正确的参数struct。
这种映射很好地映射到具有Dependent属性的类,因为有很多模型参数只从几个输入生成。
但是当我尝试从struct
知名度生成class
时,我们的视角不受尊重:
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,以便能够从生成的代码中动态设置模式。
答案 0 :(得分:1)
publicProperties = properties(x);
myStruct = struct();
for iField = 1:numel(publicProperties), myStruct.(publicProperties{iField}) = []; end
答案 1 :(得分: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