Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Could you please explain me the Matlab code below? What is the reason of using the Structure in Constructor function?
Why there is a if-else check for StructParameters.noInput? What is the purpose this?

Many thanks!!!

classdef myClass
    properties
        a;
        d;
        noInput;
    end
    methods
        function obj=myClass(StructParameters)
            if nargin==0
                StructParameters.noInput=1;
            else
                StructParameters.noInput=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, 'a')
                obj.a=[1 2; 4 5];
            end
   
           if ~isfield(StructParameters, 'd')
                obj.d=[0 1; 4 5];
            end
        end
       
     end
   
end

What I have tried:

Made a search through Google and some other Forums but still in difficulties understanding the usage of Structure in Constructor function.
Posted
Comments
RedDk 19-Jun-17 13:26pm    
I'm no engineer only a minion and I don't use MATLAB nor profess to have any insights into material "Struct(s)" but just looking at this code block I'd assume you're asking about this whole initialization called "myClass". In that case there either is or isn't any parameter and the initialization will end according to that first test (noInput). Secondly, getting 'a' is perfunctorily objectified somehow by the thing that initializes this class; to be differentiated by the presence of 'd' that is. ... I'm not sure the above is really a question anybody can answer here. So, try StackExchange for MATLAB? Input keywords from your installation help, preferably pairing concepts which are the biggest mysteries, etc ...
cpengineer 21-Jun-17 11:19am    
Thank you for your reply and comments RedDk.
In the following if-else-end statement, the values (1 and 0) of
StructParameters.noInput are just a binary to indicate there is an input or not?

if nargin==0
                StructParameters.noInput=1;
            else
                StructParameters.noInput=0;
            end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900