How to pass the data in a data structure into a constructor function?

3 ビュー (過去 30 日間)
Tuncay Eren
Tuncay Eren 2017 年 6 月 21 日
編集済み: per isakson 2017 年 7 月 12 日
I am trying to understand the data structure usage in MATLAB constructor function and below is the code which has a basic avarage function. The object has 3 properties and 2 of them are used for avarage calculation. I understand the F is being used to obtain the structure but th previous code line below is still not clear so I am trying to understand below part between (if-else-end) statement.
if nargin==0
StructParameters.EmptyInput=1;
else
StructParameters.EmptyInput=0;
end
.
classdef Class
    properties
        k;
        l;
        EmptyInput;
    end
    methods
        function obj=Class(StructParameters)
            if nargin==0
                StructParameters.EmptyInput=1;
            else
                StructParameters.EmptyInput=0;
            end
           
            if ~isstruct(StructParameters)
                error('Input argument has to be struct.')
            else
                % load the parameters
                F=fields(StructParameters);
                for x=1:length(F)
                    obj.(F{x})=StructParameters.(F{x});
                end
            end
                    
            if ~isfield(StructParameters, 'k')
                obj.k=8;
            end
   
           if ~isfield(StructParameters, 'l')
                obj.l=4 ;
            end
        end
%
function m=avarage_function(obj)
m= (obj.k+obj.l)/2;
end
     end
 end
  4 件のコメント
Adam
Adam 2017 年 6 月 21 日
Are you sure you ran all these tests under the same conditions and that this class (whose name is terrible by the way - give it a proper meaningful name!) is on your path?
The errors you get in the latter two cases are indicative of it not finding 'Class' on your path so it never gets as far as running the constructor. Otherwise your tests look fine to trigger the behaviour you were looking for.
The if statement at the top (which I assume is just pulled out from the one in the class since it is not legal syntax to have that at the top of a class file above the classdef is a very ugly way of just ensuring you have a struct. The field it sets is actually unused in the rest of the code so it really is just there to create 'any struct' essentially if one wasn't passed in.
For a class whose constructor apparently only needs two inputs this seems a very overblown design. Putting input parameters in a struct can make sense if there are lots of them, but when there are just two they should simply be passed in themselves, and as expected arguments.
I don't see any advantage to a class that calculates an average being able to be created without arguments and defining arbitrary defaults, but that is an aside I guess.
Tuncay Eren
Tuncay Eren 2017 年 6 月 21 日
I am actually working on OFDM simulation and there are lots of OFDM signal parameters in my simulation. I am building my code using object oriented paradigm and to do so I use data structure to assign the data. But before doing this study I am trying to understand how to use data structures in a constructor function and hence I have put this small example code in my question since it will be easy for me to understand the concepts over this small design.
You are right, the directory which I run the following commands was not in the same condition. I just realized it.Sorry.
Below are the outputs from both cases.
>> StructParameters=5;
>> w=Class(StructParameters)
Field assignment to a non-structure array object.
Error in Class (line 15)
StructParameters.EmptyInput=0;
>> StructParameters.y=12;
>> StructParameters.b=14;
>> w=Class(StructParameters)
No public property y exists for class Class.
Error in Class (line 26)
obj.(F{x})=StructParameters.(F{x});

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by