How to run a function which is not on the path?

41 vues (au cours des 30 derniers jours)
K E
K E le 8 Oct 2012
I would like to run a function which is not on MATLAB's path. Formerly I did this with evalf, but once I upgraded to R2012b, the program fails here:
str2func(funcNam)
Warning: The input to STR2FUNC
"S:/source/matlab/utils/graphics/distinguishable_colors" is not a
valid function name. This will generate an error in a future release.
So, is there another way to run a function if the function is not on the path? While it is possible to run a script which is not on the path, that doesn't help because I need the function's outputs. I don't want to add the directory to my path because it has so many directories that it is causing issues with performance. I suppose I could just change to the function's directory, run it, and change back to my initial directory. But perhaps there is a better solution.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Oct 2012
Changing directories is your only option.
You can edit the code for run. There is no reason that I could see that run() could not be adapted to return values.
Do note, though, that if you are relying on anything in the function being called relies on a function found in the current directory as of the time you call the function, then you are going to have problems; you would also have problems if the new directory "shadows" functions in a different way than the other directories do.
  3 commentaires
Guillaume
Guillaume le 7 Nov 2022
Is there a way to run a personnal function not using the changing path with Matlab new releases ?
Walter Roberson
Walter Roberson le 7 Nov 2022
No, the situation has not changed. Functions must be on the path, either directly or because they are methods of a class that is on the path, or because they are import() from a package that is on the path.
The only variance from this is that once you have created a function handle to a function on the path then the function does not need to stay on the path as long as you use the function handle. So you could cd to the directory, take a handle, cd back, and use the function handle.
There is no call anything like
results = feval('C:\User\GFLUER\DESKTOP\MATLAB\project6\sum.m', x, y, z)
run() is the only function that accepts a path, and run() does not handle functions. run() works internally by using cd() then executing the script and then cd back, with a layer in there to import and export variables to the calling function.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by