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

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 Comments

I'm not really clear which bit you don't understand. There are two if-else-end statements and your comment about the 'previous' line of code comes after discussing what F is, but the previous line from where F is defined is trivial.
So to give a general description of the constructor's actions:
  1. If constructor was called with no arguments then create a default struct and field. From this point on StructParameters is guaranteed to exist
  2. If you don't have a struct as input then throw an error
  3. If you do have a struct then get its field names as a cell array.
  4. For each field name assign the value of that field to the same named property in the object. (For a function that does a bunch of validation this part seems to just rely on trust that the struct does not contain any fields which don't have a correspondingly named property in the class
  5. If the 'k' and 'l' fields did not exist on the struct then assign default values for these in the object.
An example of using dynamic strings for accessing field names (equivalently properties in a class) is given here. It is very useful and I have used it a few times, but beware of over-using it. I would advise to use it sparingly as things that access properties using these dynamic methods can be difficult to follow when reading code so too many of them will likely confuse you!
Thank you for your reply and comments. I was actually asking for the if-else-end statement on the top of classdef line. But I think my understanding is better now. If I understand correctly in order to be able to run this code as designed and as for the purpose of the average function, we need to put the data first into the data structure named StructParameters, to do so as an example I take the following commands below:
>StructParameters.k=10; %%the value for the field a
>StructParameters.l=20; %%%%the value for the field d
>w=Class(StructParameters) %%Initializing the object 'w' via the constructor function,the object is now created from the Class.
>w.avarage_function %%with the values of the parameters (k,l), the function is run and the output is calculated.
But I am still not clear about the followings:
1-If I set the StructParameters to any value as follows then I expect the error function should run and throw the error ('Input argument has to be struct) since the StructParameters is not a struct, just a value assigned on it. But I get the output as:
>> StructParameters=5;
>> w=Class(StructParameters)
Undefined function 'Class' for input arguments of type 'double'.
So how can I reproduce this error condition?
2-In your comments above in 5 (If the 'k' and 'l' fields did not exist on the struct then assign default values for these in the object.) Now I generate the structure (StructParameters) but different field names (instead of 'k' and 'l'), it doesn't assign default values (k=8 and l=4).
>> StructParameters.y=12;
>> StructParameters.b=14;
>> w=Class(StructParameters)
Undefined function 'Class' for input arguments of type 'struct'.
I would be glad I you could also explain the situations above.
Thanks
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.
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});

Sign in to comment.

Answers (0)

Categories

Asked:

on 21 Jun 2017

Edited:

on 12 Jul 2017

Community Treasure Hunt

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

Start Hunting!