Call function by path or namespace?

41 ビュー (過去 30 日間)
Ted Shultz
Ted Shultz 2019 年 8 月 29 日
コメント済み: Ted Shultz 2019 年 9 月 4 日
I have a package that was given to me that has some functions that have the same name as built in MATLAB functions, preventing me from calling the built in functions. I can image a similar situation where I have been given several packages with functions of the same name. Is there some way to call a function by path (or specify which function I want), or to specify a name space? I did find this similar question, where the suggested solution is to rename the functions, but logistically that can be difficult for packages I don’t control.

採用された回答

per isakson
per isakson 2019 年 9 月 4 日
編集済み: per isakson 2019 年 9 月 4 日
That's a good question. These two Matlab blogs
touch upon the topic. If nothing else they suggest that there is a real problem that lacks a good and simple solution.
You write "packages", but that word is already taken by Matlab, see Packages Create Namespaces.
"rename the functions, but logistically that can be difficult for packages I don’t control" indeed and when a new version of the package appears ... .
I have a suggestion that's workable in some cases
  • the package has a simple folder structure; not a lot of nested subfolders
  • the name collisions are caused by ordinary functions; not classes or package folders
  • the package shall permanently be on the Matlab search path
Move the functions that cause collosions to new or existing private folders. Something like
stat_package
code
...
+package_folder
private
private
mean
sum
It might be necessary to put copies of the a function that cause collosion in more than one private folder.
  1 件のコメント
Ted Shultz
Ted Shultz 2019 年 9 月 4 日
編集済み: Ted Shultz 2019 年 9 月 4 日
Module appears to be the solution to my problem. Thank you for this information!
--ted

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

その他の回答 (2 件)

Jim Riggs
Jim Riggs 2019 年 9 月 3 日
編集済み: Jim Riggs 2019 年 9 月 3 日
One way to approach this is to create a separate folder (that is not on the Matlab path) for your project.
When you set the Matlab working directory to this folder, Matlab should search this folder first, before anything else on the Matlab path, and it will find the locally-defined functions first.
Another approach is to manage the Matlab search path by adding the folder that contains these functions to the beginning of the search path. Matlab will find them first.
This second approach requires you to un-do the path assignment when you are running other projects, so you have to be careful. You can use setup scripts to manage the path changes.
  1 件のコメント
Ted Shultz
Ted Shultz 2019 年 9 月 4 日
編集済み: Ted Shultz 2019 年 9 月 4 日
Thank you for your reply. I am resistant to just modify the path, as I would like to be able to run both functions in the same program. The file exchange program "module" appears to let me call either.

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


Steven Lord
Steven Lord 2019 年 9 月 4 日
I have a package that was given to me that has some functions that have the same name as built in MATLAB functions
I'd be curious why the author of that package / toolbox / collection of files shadowed built-in MATLAB function (or functions implemented as MATLAB code that's part of the MATLAB installation.) Others have suggested ways to work around this problem, but the root cause of this pain seems to me to be the shadowing and the countermeasure is for the package author to rename those functions.
If that's impossible and the functions being shadowed are truly built-in (not just MATLAB code files provided by MathWorks as part of your MATLAB installation) the builtin function may be of use. But I'd still strongly encourage the author to stop shadowing built-in functions.
  1 件のコメント
Ted Shultz
Ted Shultz 2019 年 9 月 4 日
This has happened to me in the past when I have old code that has functions that are older than the built in functions, or when I have been giving code by someone who doesn’t have the same packages as I have.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by