2016-02-16 77 views
0

我需要定義一個稱爲MobileBaseStation類和被叫DataChannel屬性,它是一個結構如下所示,製作一個結構屬性的字段作爲類的屬性在Matlab

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 

的制定者需要強制DataChannel結構的字段的邊界/限制。我希望結構的字段是MobileBaseStation類的屬性,以便我可以使用setter。我如何在Matlab中實現這一點?

+1

創建'DataChannel'一個新的類,並作出這樣的類的屬性你的'MobileBaseStation'類 –

回答

0

我想你想使DataChannel私人所以你可以通過你的依賴屬性獲取控制訪問&制定者,如:

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 
相關問題