Does specific .m define particular function?
이전 댓글 표시
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. :(
채택된 답변
추가 답변 (1개)
Arnab Sen
2016년 2월 23일
0 개 추천
Hi Walter,
The inbuilt function 'exist' can help you in this regards. It returns various code based on entity present as the given name in MATLAB path.
For more details about 'exist', refer to the following link:
댓글 수: 1
Walter Roberson
2016년 4월 17일
편집: Walter Roberson
2016년 4월 17일
카테고리
도움말 센터 및 File Exchange에서 Functions에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!