Create function handle from function file
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
Star Strider
2020 年 3 月 10 日
@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.)
Arthur Roué
2020 年 3 月 11 日
@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.
Star Strider
2020 年 3 月 11 日
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;
Star Strider
2020 年 3 月 11 日
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.
Arthur Roué
2020 年 3 月 11 日
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
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 についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
