【问题标题】:Making the fields of a struct property as properties of the class in Matlab在 Matlab 中将结构属性的字段作为类的属性
【发布时间】:2016-02-16 21:07:27
【问题描述】:

我需要定义一个名为 MobileBaseStation 的类和一个名为 DataChannel 的属性,它是一个如下所示的结构,

classdef MobileBaseStation
properties
    DataChannel = struct('TxScheme','SpatialMux','NLayers',4);
end
properties (Constant = true)
    supportedTxSchemes = {'Port0','TxDiversity','CDD','SpatialMux','MultiUser','Port5','Port7-8','Port8','Port7-14'};
end
methods
    function this = MobileBaseStation(this,TxSchemeChoice,NLayers)
        this.DataChannel.TxScheme = TxSchemeChoice;
        this.DataChannel.NLayers = NLayers;
    end
    function this = set.DataChannel.TxScheme(this,value)
        if ismember(value,this.supportedTxSchemes)
            this.DataChannel.TxScheme = value;
        end
    end
    function this = set.DataChannel.NLayers(this,value)
        if strcmpi(this.TxScheme,'Port8') && value==1
            set.DataChannel.NLayers = value;
        end
    end
end
end

setter 需要对 DataChannel 结构的字段实施边界/限制。我希望结构的字段成为 MobileBaseStation 类的属性,以便我可以使用设置器。如何在 Matlab 中实现这一点?

【问题讨论】:

  • DataChannel 创建一个新类,并使该类成为MobileBaseStation 类的属性

标签: matlab oop matlab-class


【解决方案1】:

我认为您希望将 DataChannel 设为私有,以便您可以通过依赖属性的 getter 和 setter 控制访问,例如:

classdef MobileBaseStation
    properties(GetAccess=private, SetAccess=private)
        DataChannel = struct('TxScheme','SpatialMux','NLayers',4);
    end

    ...

    properties(Dependent=true)
        TxScheme;
        NLayers;
    end

    methods
        function v = get.TxScheme(this), v = this.DataChannel.TxScheme; end
        function v = get.NLayers(this), v = this.DataChannel.NLayers; end

        function this = set.TxScheme(this,v)
            assert(ismember(v,this.supportedTxSchemes),'Invalid TxScheme - %s.',v);
            this.DataChannel.TxScheme = v;
        end

        ...
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2016-07-30
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多