Must a Function be in Scope Before Creating a Handle to It?
3 views (last 30 days)
Show older comments
"Keep the following in mind when creating handles to functions:
- Scope — The function must be in scope at the time you create the handle. Therefore, the function must be on the MATLAB path or in the current folder."
Trying to verify that statement ....
Verify a proposed function name is not currently in scope
exist xyzzy
But we can create a handle to this non-existent function (seemingly contra to the doc page)
f = @xyzzy;
Error is generated if trying to use the handle, of course.
try
f()
catch ME
ME.message
end
After generating the handle we can create the function (typically through the editor, or renaming an existing file, or ....)
str = ["function a = xyzzy";"a=1;"];
writelines(str,"xyzzy.m");
type xyzzy.m
And now the function handle executes
f()
Am I misunderstanding something about that doc page, or is the the doc page wrong, or does the doc page reflect how Matlab is supposed to work and the preceding code indicates a bug of some sort?
0 Comments
Accepted Answer
dpb
on 16 Aug 2025
Edited: dpb
on 16 Aug 2025
I believe that particular item in the documentation was written with an unstated (and perhaps not recognized having done) implicit assumption by the author that the subsequent created function handle is valid to be exectued immediately after the the handle is created whereas, as your code demonstrates, a handle to a non-existing function can be created, just not used until a function matching the prototype does exist and is in scope.
Normally, as the documentation states, the function handle contains the complete path to the function; however, if it does not exist then the path to it doesn't exist, either. But, when/if a function handle variable is store'ed, the file path is not save'ed, either, so if/when reloaded the (perhaps formerly valid) handle will also be invalid unless the function is in scope at that time.
Probably some amplification of the documentation would be in order, but I believe the behavior is as designers intended. Of course, with the propretary language, whatever the actual design specification says isn't available to be studied for conformance nor is there a formal procedure to ask for a standards review of an arcane language corner as with languages with official Standards.
7 Comments
Steven Lord
on 22 Aug 2025
I've always wondered if the developers review the documentation to make sure intent and functionality is documented correctly.
Yes, they do. The developers work closely with the documentation staff.
If the documentation doesn't cover all possibilities, then the Matlab user doesn't know if code that uses an undocumented possibility is working as intended or not.
In cases like that, it's perfectly reasonable to submit a request to Technical Support asking 1) if the functionality is working correctly / as intended and/or 2) for it to be included in the documentation.
Writing documentation that is complete and entirely accurate might not be easy, but it is important to strive to do so, IMO.
The PDF documentation for MATLAB has a book, MATLAB Function Reference, that includes the reference pages for all the documented functions that are part of the product MATLAB (as far as I'm aware.) It doesn't go into all the details for all the functions. I strongly doubt any one person has read through it, cover to cover. [I certainly have not. Maybe some AI system has included it in its training set, but that's different.]
Why do I doubt this? It's 19828 pages long! For comparison, one of the prototypical "long books" that people usually refer to when they want to emphasize something taking a long time to read is War and Peace. The function reference book is over 16 War and Peaces in length.
functionReference = 19828;
warAndPeace = 1225;
lengthOfFunctionReferenceInWarAndPeaces = functionReference./warAndPeace
I don't know how much longer a document it would be if it discussed every detail, edge case, and corner case of every function. Twice as long? Three times? How much of that document would people actually read?
OTOH, Mathworks changes how stuff works virtually every release. Of course, they also introduce new (and often very useful) features regularly as well.
If you wanted to be technical, we change "how stuff works" every single release. Every bug fix, every new function, even fixing a crash is technically at least a potential backwards incompatibility. [See the obligatory XKCD.] I seem to recall recently people complaining on Answers that we had caused problems in their code when we introduced the settings function a few years back and started shadowing their functions by that name, as one example. We do take compatibility into consideration when reviewing the designs for new features/functionality and often do when reviewing bug fixes as well.
dpb
on 25 Aug 2025
Thanks for the comments, Steven...
Is there anything in the PDF documentation for MATLAB book, other than the content of the online documentation reformatted? I had always presumed they were identical content, the book just being created from the other doc so there wasn't really anything to be gained by wading through it line by line. (Although I'll note the page count is very high in part owing to the formatting that every section begins a new page while Tolstoy's publishers didn't begin each paragraph on a new page <vbg>)
One thing compared to the Programmers Reference Manual for a language such as Fortran is that MATLAB includes so much more functionality by bundling all the math matrix algebra, diff-eq, and graphics functions, etc., etc., etc., into the one package as opposed to also having to document IMSL, BLAS/LAPACK, etc., etc., etc., which also adds up to something approaching at least similar volume.
I'm still curious, though, about how MATLAB actually documents the design requirements -- does TMW have its own Standard language like J3 or similar or do they have another way of actually writing the specification...
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!