Call a function in a sub directory?

44 ビュー (過去 30 日間)
TallBrian
TallBrian 2019 年 12 月 6 日
回答済み: Walter Roberson 2019 年 12 月 6 日
Suppose a have a folder "folder1" which contains a function "sayhi.m". "folder1" contains a subfolder "subfolder1" which contains an identically named function "sayhi.m".
Is it possible to call both functions from a function in the root folder? Or, how can I override the default call to the local function and force the call of the non-local function?
thanks!

回答 (2 件)

Matt J
Matt J 2019 年 12 月 6 日
編集済み: Matt J 2019 年 12 月 6 日
The only way, I believe, is to visit each folder and create a function handle to each respective version of sayhi(),
cd folder1
h1=@sayhi;
cd folder2
h2=@sayhi;
cd ../
Once you have the handles h1() and h2(), you can invoke them from whatever folders you wish.

Walter Roberson
Walter Roberson 2019 年 12 月 6 日
cd to the inner directory and create a handle to the function, and cd back. Now when you invoke the handle you get the inner function no matter what your current directory is

カテゴリ

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