MATLAB: How to access subfunctions from outside the main function in MATLAB

accesscexternalfunctionimportincludeMATLABsubfunctionworkspace

I have a function file that contains some subfunctions, as in the example below:
function foo1
disp('This function contains subfunctions it can call.')
foo2;
foo3;
function foo2
disp('in foo2');
function foo3
disp('in foo3');
I would like to access subfunctions "foo2" and "f003" from outside the function (similar to the "include" functionality in C).

Best Answer

The ability to access subfunctions from outside of a function file via an "include" functionality is not available in MATLAB.
To work around this issue, a main function can provide a structure of subfunction function handles as an output argument. As an example, modify the function "foo1" as follows and save it in a file named foo1.m:
function fun_api = foo1
fun_api.foo2 = @foo2;
fun_api.foo3 = @foo3;
function foo2
disp('in foo2');
function foo3
disp('in foo3');
Subfunctions foo2 and foo3 can now be called from outside of foo1.m in the following manner:
fun_api = foo1;
fun_api.foo2();
fun_api.foo3();