Saving my own function to be used as MATLAB built-in functions

I would like to write a function that can be called from anywhere regardless of my current working path, just like I use integral(...) or sin(...). How should I change the setup to be able to do this?

 採用された回答

Ahmet Cecen
Ahmet Cecen 2018 年 4 月 26 日
編集済み: Ahmet Cecen 2018 年 4 月 26 日

1 投票

Create a startup.m file in your MATLAB user folder(usually Documents/MATLAB) then use addpath in that file.
MATLAB will execute that file everytime it starts up, meaning the paths you specify in that file will be "permenantly" available in any folder for all intents and purposes. (At least for all cases someone asking this question will likely encounter.)

その他の回答 (1 件)

the cyclist
the cyclist 2018 年 4 月 26 日
編集済み: the cyclist 2018 年 4 月 26 日

2 投票

One way is to put any such functions into a particular directory (e.g. "utilities"), and then add that directory to the search path using the addpath command. I put the addpath command in my startup.m file, so it gets called every time I start MATLAB.

4 件のコメント

Jan
Jan 2018 年 4 月 26 日
Instead of putting addpath to the startup file, you can use savepath or the GUI pathtool to store the folder permanently.
Saeid
Saeid 2018 年 4 月 26 日
I tried this:
savepath D:\Documents\MATLAB\Examples
and got this message:
Warning: Unable to save path to file 'D:\Documents\MATLAB\Examples'.
You can save your path to a different location by calling SAVEPATH
with an input argument that specifies the full path. For MATLAB to
use that path in future sessions, save the path to 'pathdef.m' in
your MATLAB startup folder.
> In savepath (line 145)
Jan
Jan 2018 年 4 月 27 日
A correct usage of savepath is:
addpath('D:\Documents\MATLAB\Examples', '-end');
savepath
Then the path is saved to the matlabroot folder, but this requires admin privilegs. Therefore it might be better to follow the advice given in the warning message: save the pathdef.m file to your startup folder - the folder which is active initially.
It is not clear, why the saving failed in your case. Is the folder existing and do you have write privileges?
Saeid
Saeid 2018 年 4 月 27 日
Thanks!

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

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

質問済み:

2018 年 4 月 26 日

コメント済み:

2018 年 4 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by