Create function handle from function file
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2020년 3월 10일
편집: Steven Lord
2020년 3월 10일
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é
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.
@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에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
