Is it possible to create a function handle from function file ? In other words, create a function_handle object @foo from file foo.m.

 채택된 답변

Star Strider
Star Strider 2020년 3월 10일
편집: Steven Lord 2020년 3월 10일

0 개 추천

Yes!
In some instances, that is necessary in order to evaluate the function.
Example —
function y = f(x)
y = x.^2 - 2;
end
fz = fzero(@f,1)
produces:
fz =
1.4142
[SL: typo fix]

댓글 수: 8

@Steven — I obviously didn’t pick up the typo. Thanks for correcting it, whatever it was! (On that note, it would be nice to have Answers and Comments be able to be checked by the browser spell-check utility.)
@Star Strider - Thank you for you answer. In fact I should have been a bit more specific, my function is not in my path, so I need to point it with absolute path and i couldn't find a way to execute a command like 'C:\SomeFolder\MyFun.m' as a function.
Actually, I find another way to do what I wanted, but I'm still interested if there is anyway to create a function handle from function file that is not in search path.
As always, my pleasure.
I never encountered the possibility of referring to a function or script file that was not in the search path. (This is relatively easy to do with data by using fullfile to create the path to it.) The only solution I can think of is to use the addpath funciton to add that directory to the MATLAB search path.
What way did you find to do what you want?
Arthur Roué
Arthur Roué 2020년 3월 11일
편집: Arthur Roué 2020년 3월 12일
I used cd to change the directory for the one containing my function (way simpler, don't know why I didn't think of it). The reason why I needed to do this is because the code is running in another instance of MATLAB launch with the actxserver function.
Here is a part of the code :
% Create MATLAB session
hMatlab = actxserver('Matlab.Application.9.1');
% Tests directory
repTests = fileparts(which(mfilename));
% Change directory
hMatlab.Execute(sprintf('cd(''%s'')', repTests));
% Run tests
hMatlab.Execute('runTests();');
% Close session
hMatlab.Quit;
Interesting. Thank you for posting that.
The function for which you want to create the function handle must be in scope when the function handle is created. It does not need to be in scope when the function handle is evaluated.
function fh = myTimesThree
fh = @localTimesThree;
function y = localTimesThree(x)
y = 3*x;
end
end
You can run myTimesThree then call the function handle it returns even though (because it is a local function inside the file) you couldn't call localTimesThree directly.
f = myTimesThree;
y1 = f(1:10)
y2 = localTimesThree(1:10) % Throws an error
So if you made the function not on the path in scope to MATLAB temporarily (by using cd or addpath) and created the function handle while it was in scope, you could then call that function using the function handle even if you've changed back to a different directory and/or restored the previous path.
Arthur ROUÉ -- I'm curious why you're running the tests in a separate MATLAB session. If your runTests function is a wrapper around the built-in runtests function that's part of the unit testing framework included in MATLAB you could specify the tests input to runtests as the name of the folder containing your tests.
Thanks for your answer Steven Lord, it's a nice way to call a function not in scope.
I do use the testsuite call to run my tests. The reason why I run it in a separate MATLAB session is because I also loop on MATLAB versions to ensure the compatibility ot the toolbox I created in several MATLAB versions.
tommsch
tommsch 2020년 11월 30일
@Steven Lord
The function for which you want to create the function handle must be in scope when the function handle is created. It does not need to be in scope when the function handle is evaluated.
When the function is shadowed by another function later on, the function handle may point to another function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2020년 3월 10일

댓글:

2020년 11월 30일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by