can a function be made to be called from any directory?

조회 수: 22 (최근 30일)
hxen
hxen 2025년 3월 24일
댓글: hxen 2025년 3월 24일
I have custom functions that I would like to be called independent of the directory it is called. Can this be done, like for the MATLAB functions? Can someone steer me to an example or page to learn about this if it is possible?

채택된 답변

TED MOSBY
TED MOSBY 2025년 3월 24일
Hi Hxen,
If you are referring to MATLAB custom functions be able to be called from anywhere, the key is to place your '.m' files in a folder that is on the MATLAB search path. Once the folder is on path, you can call it from any script, function, or the Command Window, no need to 'cd' into that folder.
You can do this by running below commands in the command window:
addpath('full/path/to/your_folder')
savepath
You can also do the same by right-clicking on your folder and selecting the 'Add to Path' option.
Here is the documentation of 'addpath' :
Hope this helps!

추가 답변 (1개)

Walter Roberson
Walter Roberson 2025년 3월 24일
If you are referring to custom functions that are to be called no matter which directory you call them from, then the answer is "That cannot be done" -- not unless you "import" the name.
Object functions are only #7, and Class constructors are only #8, and Functions elsewhere on the path, are only #11 -- which is lowest precedence.
So no matter what name you give to the function or how you declare the function, there are cases where other functions with the same name will have higher priority... unless you "import" the name.
  댓글 수: 1
hxen
hxen 2025년 3월 24일
Thanks Walter and bringing to my awareness of the ordering. Much appreaciated!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by