How to use call helper functions for the definition of constructor method in user-defined class?
    16 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Marco
 am 12 Jan. 2023
  
    
    
    
    
    Kommentiert: Marco
 am 15 Jan. 2023
            I have created the following class and I am describing his constructor method using helper functions: (here reporting only a snippet of all the code)
classdef RESOURCES
    properties
        xyz
    end
    methods
        function obj = RESOURCES( arguments ) % this is the constructor
            % helper functions call
            [obj.xyz] = helper_function(obj, arguments);
            ...
        end
        %% helper functions description
        function xyz = helper_function(obj, arguments)
            ...
        end      
        ...
    end
end    
Question 1:
Matlab syntax checker reports the following:
helper_function(obj, arguments)
[Input argument obj might be unused. Considering replacing with ~]
If I do it, the code runs as expected. But is this ok? What kind of method is helper_function then? Static?
If I remove completely obj I get a non-specified error about number of input/output arguments, though.
Question 2:
What if I want to do the same, but writing the helper function in a separate file?
thank you!
0 Kommentare
Akzeptierte Antwort
  Steven Lord
    
      
 am 12 Jan. 2023
        For your first question, if helper_function doesn't require any information about the state of the object it probably doesn't need to be an ordinary method. I likely would either make it a static method (particularly if it needs to be callable by code outside the class definition) or a local function in the class file.
If you changed its definition of helper_function(~, arguments) and call it with an instance of your class as the first input, MATLAB will dispatch accordingly. You just won't be able to work with that class instance inside the helper_function as it was not assigned to a variable in that function's workspace.
And yes, there is no "implicit this" type argument in MATLAB classes. These two lines of code behave almost identically:
helper_function(obj, arguments)
obj.helper_function(arguments)
They can differ if you have multiple objects with class precedence involved. But that's kind of an advanced maneuver.
For your second question, if you wanted to define helper_function in a separate file I wouldn't make it any type of method. Just make it a plain old function (and if it doesn't require an instance of your class as input, I wouldn't include obj in the function definition.) If the helper_function was not intended to be called directly by users I'd consider making it a private function. If you have your class in an @ directory and want the function to be private see this documentation page for more information.
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

