How to access a function under a given path?

86 views (last 30 days)
Greetings,
I am wonderin whether it is possible to call a function that is not located in the current directory, nor in a subfolder nor on the matlab path.
I want to call a function (with argument) based on the absolute path, where this funtion is located
Lets say I have a path saved in a variable, and I want to use the function
targetfunc
with the input argument 5.
targetdir = "A:\EI\OU";
Things like
targetdir\targetfunc(5)
obviously dont work, neither does
run
(which is made for scripts?)
The background of this is, that a script A shall clone a GIT repository to a specific folder ( another one in the same parent folder for now) and use a function that is located there.
  • Adding the targetdir to path would work, but I want to try to work around this method, to minimize the risk of getting names double on the path
  • Always creating an absolute pah to the function, that is called: I dont know how to call a function with an absolute path??
  • changing current directory might also work, but afaik this is also bad style and opens the way for problems if I use other functions/scripts in the original script (A).

Accepted Answer

TADA
TADA on 17 Jul 2019
A possible solution would be to make every git branch a package. You will have to make all subfolders packages as well, but that clears the risk of duplicate names.
It also solves the problem of calling function in specific folder.
Matlab decides what function to call in case of duplicate names according to these rules, so calling a function in a specific folder is impossible as far as i know
Lets say you have a branch called "ver1" with a folder called "common" in that folder there is an .m file function called "foo1"
So you create the new .m file in this path: "root path\+ver1\+common\foo1.m"
Now to invoke this function you will have to use the package tree like that:
ver1.common.foo1();
Also works with function handles:
fooHandle = @ver1.common.foo1;
For this to work you will have to add the root path of all branches to the mat path, and to rename all folders to have "+" at the beginning so that matlab creates a package
  5 Comments
mahoromax
mahoromax on 18 Jul 2019
Edited: mahoromax on 18 Jul 2019
Yeah, checking for PATH isnt hard, but whether it is a subfolder with + = package inside a folder on PATH is getting complicated. Especially since I cant just split at the first + but have to check whether
a) folder parts before + is on PATH (easy, the ismember variant seemed the smoothest to me)
I split my currentfolder along its '+'es, and add the '+' back to all but the first entry, then check the first one for membership of PATH
b) every folder afterwards starts with '+':
I take the 2nd till end entries, but them back together, split them along filesep, then I test the rest whether the content of each cell starts with a +....
I'll also take a look at that packaing / moduling!
-> That seems really useful. But thinking about my project. Lets say I start with my script A and GIt repos B,C and D
run A -> it clones B to a folder "+B", calls sth like a B.main which clones C into "+B/+C" (if its a script?) or "+C" (if function?) and so on
If I wanted to run a function of C that uses another function of C... I would have to program all functions in C using the module namespace. Maybe stuff like that could be automated before pushed to git... but it seems a lot of trouble to teach multiple ppl atm :-/
Atm I am favouring the idea of a) cloning into subpackages, but then changing current directory into the specific subpackage within a script that is calling sth inside the subpackage
Last Edit:
I will probably - you guessed it - just add my Git repos to path, if there's already functions with the same name on path it should take the ones added later. So as long as I remove my folders from path again there should be no issues.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!