8

I have a local function defined in an m-file. For example:

% begining of public_function.m file
function fh = public_function( )
%
% do some computation...

fh = @local_function; % return function handle to local function defined below

function y = local_function( x )
% 
% a local function inside public_function.m file
% 

% some manipulation on x
y = x;

% end of public_function.m file NOTE THAT local_function is NOT nested

Now, I would like to call local_function from command line (and not from public_function). I was able to do so using the function handle returned from public_function:

>> fh = public_function(); % got handle to local_function
>> y = fh( x ); % calling the local function from command line :-)

My question:
Is there any other way (apart from explicitly pass the function handle) to call local function from command line (or other m-file/functions)?

More precisely, I want a method to access any local function in a file (provided that I know its name). So, if I have public_function.m file (and function) and I know that local_function is local to that file, is there a way to access local_function from command line ?

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 2
    You can pass the name of the sub-function as an argument of the main function. See the second example: http://www.mathworks.de/support/solutions/en/data/1-19LN6/?solution=1-19LN6 – H.Muster Mar 17 '13 at 15:11
  • Did you check mklib at the matlab fileexchange? It automatically creates handles to any local function in a file you list as function argument: http://www.mathworks.de/matlabcentral/fileexchange/7597-mklib-a-pedestrian-function-library-loader-generator – H.Muster Mar 18 '13 at 15:17
  • A better question is why you're making the function a local function if you have a pressing need to call it from outside of an m-file. The idea of making something a local function is to limit its scope. Perhaps you should put it in its own m-file, or make it a private function. I suggest taking a look at [this question](http://stackoverflow.com/q/3569933/52738) for more info. – gnovice Oct 16 '13 at 18:36
  • 1
    I don't think is an unreasonable thing to want. If a function f is used by only 1 other function, then it lives as a local function to have its scope limited. The problem here is that if f is used by n > 1 other functions, then it must live in a global namespace. For large projects, this is clearly untenable of a solution. – Derek Jan 14 '15 at 12:16

1 Answers1

5

The official documentation says that:

... you cannot call a local function from the command line or from functions in other files.

According to this, you must pass its handle to the caller in order to allow invoking it indirectly outside its m-file. I believe that there is no documented sensible way to access local functions otherwise.

Oddly though, you can still do this with help:

help public_function>local_function
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • I am quite hoping on fishing some undocumented way to work around this "you cannot call a local function..." something along the lines of the `help` behavior... – Shai Mar 17 '13 at 16:18
  • 1
    @Shai Yeah, me too. Even though I posted this answer, I'm still looking. – Eitan T Mar 17 '13 at 16:30
  • @Shai: maybe you can add what you are trying to achieve in the end? Are you looking for a way to access any local function in arbitrary files, or do you want a "master function" with lots of sub-functions for everyday use? – H.Muster Mar 18 '13 at 11:05