how to call a nested function from a method

48 views (last 30 days)
Hi,
I'm working on a OOP-Script and was wondering how nested functions in Methods behave.
I don't have a particular example yet, I just played around a little and coudn't make it work.
I imagine somethin like this:
classdef example
properties
one
two
end
methods
%Constructor
function obj = example(1,2)
obj.one = 1
obj.two = 2
end
function parent(obj)
X = 1 + child
function child(obj)
[...]
end
end
end
end
I tried to call the "child" function from a different script to see it's value but always got errors like "no static Method or propertie" or "not enough input arguments"
I tried calling it like
example.parent.child(1,2)
example.child(1,2)
@example.child
and a couple other options.
My try and error progress got me thinking if this is even possible at all?
Any tipps and tricks are highly appreciated!

Accepted Answer

per isakson
per isakson on 25 Apr 2020
Edited: per isakson on 25 Apr 2020
"how nested functions in Methods behave" the same way as in functions
"call the "child" function from a different script" the only way to call the nested function, child, from outside the method, parent, is by use of a function handle that is created inside the method, parent. There must be a good reason to use function handles this way, because it will come with surprises.
"is [it] even possible at all?" I recommend that you regard it as impossible.
"I want to Access the Value of a Child function that is NOT a Constructor..." A nested function cannot be a constructor. And a nested function inside a constructor cannot be reached from outside the constructor.
Here is a small demo based on your class, example.
>> obj = example( 1, 2 ); % create an object
>> [X,fh] = obj.parent % call the method, parent
Nested function, child
X =
20
fh =
function_handle with value:
@example.parent/child
>> fh() % call the nested function, child,
Nested function, child % by means of the function handle, fh
ans =
19
>>
>> obj.two = 20; % assing 20 to the property, two
>> fh() % the new value of two does not
Nested function, child % affect the value returned by fh()
ans =
19
>>
where
classdef example
properties
one
two
end
methods
% Constructor
function obj = example( v1, v2 )
obj.one = v1;
obj.two = v2;
end
% Ordinary method
function [X,fh] = parent(obj)
X = 1 + child;
fh = @child; % create function handle
function val = child()
disp('Nested function, child')
val = 17 + obj.two;
end
end
end
end

More Answers (1)

Steven Lord
Steven Lord on 22 Apr 2020
See the "Visibility of Nested Functions" section on this documentation page for an explanation of from where you can call a nested function.
What's your intended use strategy for the parent and child functions? Do you expect them to be something users of your class would want to call directly on your object? If so consider making them ordinary methods, like the addData method on that page or the roundOff, multiplyBy, and plus methods of the BasicClass class created on this documentation page.
Are they something that only the class is going to need to be able to call? If so consider a private method or a class-related function, both of which are described on this documentation page.
Are they something that only one particular method needs to call and that does not need to be callable outside that method? If so, nested them inside that method is okay.
  2 Comments
Leon Stolp
Leon Stolp on 22 Apr 2020
Thanks very much for all the input!
So, the Parent function is supposed to be a calculation with a lot of child functions to calculate various needed values for that Parent function. All I want to do is call the child functions to display the calculated value and maybe use it as a constraint for a optimization. There is no need for it to be changed from the script. As I write this, maybe it's as easy as creating a function handle and just use "disp"
Leon Stolp
Leon Stolp on 23 Apr 2020
Edited: Leon Stolp on 23 Apr 2020
Just to better my understanding of Classes.
In this Context, is the Class itselft viewed as a Function? i.e. if we stick to the "Visiblity of Nested Functions" documentation, would it be like this?
function A(x, y) % Main function --> Class
B(x,y)
D(y)
function B(x,y) % Nested in A --> Method
C(x)
D(y)
function C(x) % Nested in B ---> function in Method
D(x)
end
end
function D(x) % Nested in A
E(x)
function E(x) % Nested in D
disp(x)
end
end
end
And if I understand the documentation for the ordinary method correctly, it's supposed to be set as a Constructor? I want to Access the Value of a Child function that is NOT a Constructor...

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!