Must a Function be in Scope Before Creating a Handle to It?
The doc page Create Function Handle states:
"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?The doc page Create Function Handle states:
"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? The doc page Create Function Handle states:
"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? function handle MATLAB Answers — New Questions