how to call a nested function from a method
이전 댓글 표시
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!
채택된 답변
추가 답변 (1개)
Steven Lord
2020년 4월 22일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!