How can I execute a function if it exist, otherwise use a custom function?

48 ビュー (過去 30 日間)
Arturo Torres-Romero
Arturo Torres-Romero 2025 年 10 月 18 日 18:57
編集済み: Matt J 約17時間 前
knnsearch is a function from the "statistics and machine learning toolbox"
I created my own knnsearch, however, if the user has this function already I want to run it from the toolbox. if the user doesn't have it, then execute my implementation.
The problem is that matlab fails in compiler time becasue it is expecting knnsearch to exist.
by the way... I used copilot and chatgpt for suggestions.... but their suggestions didn't work.
function [k_idx, D] = knnsearch_lib(queryPoint, data, k)
% Check if the Toolbox knnsearch function is available
if exist('knnsearch', 'file') == 2
[k_idx, D] = knnsearch(queryPoint, data, k);
else
% If not available, use custom implementation
[k_idx, D] = knn_custom(queryPoint, data, k);
end
end
  2 件のコメント
Walter Roberson
Walter Roberson 2025 年 10 月 18 日 19:06
I wonder if the following would work:
testhandle = @knnsearch;
if ~isempty(functions(testhandle).file)
[k_idx, D] = testhandle(querryPoint, data, k);
else
[k_idx, D] = knn_custom(queryPoint, data, k);
end
Arturo Torres-Romero
Arturo Torres-Romero 約21時間 前
In another context I think it would work. Nice trick!
however, matlab complained:
'''Function "functions" not supported for code generation.
so, I added the command:
coder.extrinsic('functions');
but matlab then complained:
'''Function handles can not be passed to extrinsic functions.
It's surprising that a powerful language like MATLAB doesn't offer a straightforward way to handle such a common feature...

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2025 年 10 月 18 日 20:04
Have the used append (i.e., not prepend) your knnsearch to the path. If a native knnsearch exists above it in the path, it will be used instead.
  3 件のコメント
Arturo Torres-Romero
Arturo Torres-Romero 約21時間 前
I'm using MATLAB Projects, which is great because it manages files and dependencies automatically. So far, I've just added my custom files and let the project handle the rest. My question is: how can I be sure that MATLAB is prioritizing the native knnsearch function over my custom one? I'm open to renaming my custom function to knnsearch if needed — I believe that would be the optimal solution — but I want to make sure the native version is used first when available. Any suggestions on how to control or verify the order in the path?
Matt J
Matt J 約18時間 前
編集済み: Matt J 約17時間 前
Sure. To view the path
To modify it, you can also use the above, or
In your project startup script, you can use addpath and rmpath to put specific folders at the beginning or end of the path.

サインインしてコメントする。


Steven Lord
Steven Lord 約19時間 前
Are you trying to use MATLAB Compiler or MATLAB Coder? If the former, you should know that any code that you want the compiled application to run must be present at the time the code is compiled. There's no "Check if it's available at runtime, and if it isn't run some other code the user provided." or anything like that.
So if you have Statistics and Machine Learning Toolbox present in your installation and if it can be included in an application generated by MATLAB Compiler, just call it. If you don't have this toolbox (but want to provide the non-compiled version of your code to someone who may) or if it is not supported by MATLAB Compiler (I don't remember offhand if it is) you could try guarding the call in a try / catch statement or with an isdeployed call.
If you're trying to use MATLAB Coder, I'm not sure how to tell MATLAB Coder to substitute your function for knnsearch if it's not available, or even if that's possible.

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by