MATLAB: Does specific .m define particular function

functionsMATLABsymbol resolution

When I wrote programs to analyze programs, sometimes I need to know whether a specific .m defines a particular function. What are some of the options for checking that — preferably without having to parse the code for not-quoted not-commented not-block-commented occurrences of "function" ?
My most immediate use is in checking the properties of callbacks defined in objects to see whether they attempt to refer to undefined routines (which is unfortunately common.) By examining the properties (and possibly pulling apart function handles) I may have a string that is a function name and contextually I may have a good idea of which .m is most likely to define the function, but I need to cross-check automatically.
For greater certainty: this is not the case where the function to be looked for has its own .m file: this is the case where a .m file may define multiple functions, and so when executing within the .m the function name may resolve perfectly well, but I need to peak inside the .m to check.
Ideally the code resolution would handle arbitrarily nested functions, but for my immediate needs, simple top-level definitions would do. Some day I may need to think about extending this to classes, but not yet.
Alternately, it would be useful if there were a routine that could be given .m name and a target function name and the routine returned where the target function name would resolve to if executed within that .m . Better yet if the syntax allowed designating particular functions or scopes to start the resolution within.
I know that there must be some built-in functionality along these lines, as hgload() to restore .fig is known to do resolution of function handles at the time of the load, at least in some instances.
My existing code already analyzes function handles. Depending on how the function handle is created, sometimes the context of which filename is already available by using functions(), which makes things easy. But not all methods of creating function handles have non-empty "file" properties. 🙁

Best Answer

Ah, I just realized there is a way!
contains_function = @(filename, functionname) ~isempty( which([filename '>' functionname]) );
The filename can be fully qualified complete with directory and with '.m' . This does not just check to see if the file exists, it checks to see if the function exists in the file.
It will not, of course, detect functions that are dynamically created. It also cannot locate nested functions.
If you prefer to stick to documented syntax,
contains_function = @(filename, functionname) ~isempty( which(functionname, 'in', filename) );
Related Question