- If constructor was called with no arguments then create a default struct and field. From this point on StructParameters is guaranteed to exist
- If you don't have a struct as input then throw an error
- If you do have a struct then get its field names as a cell array.
- 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
- If the 'k' and 'l' fields did not exist on the struct then assign default values for these in the object.
How to pass the data in a data structure into a constructor function?
4 views (last 30 days)
Show older comments
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
Adam
on 21 Jun 2017
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.
Answers (0)
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!