MATLAB Answers

Is there a way to define a new built-in function?

1 ビュー (過去 30 日間)
Yilmaz Baris Erkan
Yilmaz Baris Erkan 2021 年 2 月 8 日
Greetings,
For plotting purposes, I simply want to have a built-in function that gives you the color of your choice. Code is well written and ready, however, I wonder if there is a way to make it built-in instead of including the .m file where ever I need.
Any advice is most appreciated.
  2 件のコメント
Stephen Cobeldick
Stephen Cobeldick 2021 年 2 月 8 日
Put it on the MATLAB path (but definitely NOT in any installation folder or any application):

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

採用された回答

Matt J
Matt J 2021 年 2 月 8 日
編集済み: Matt J 2021 年 2 月 8 日
I'm under the vague impression that you are unaware of the Matlab path and how it allows you to keep your mfiles in one place where Matlab can find them. See addpath and pathtool.
  4 件のコメント
Yilmaz Baris Erkan
Yilmaz Baris Erkan 2021 年 2 月 9 日
@Matt J thank you for the guide and explanation. I appreciate it.

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

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2021 年 2 月 8 日
I would think NO but it does not really make any difference. If there is a way, you would have to modify some existing files in the installation package or add some files to the installation foder, which is worse than adding the .m file to your custom folder, either for yourself use or for deploying to somebody else.
If you add your custom foder to the MATLAB path, you would not need to "include the .m file where ever I need". If you want to deploy to somebody else, you could also create the .p file to protect your algorithm.

John D'Errico
John D'Errico 2021 年 2 月 9 日
As others have said, you need to start using your MATLAB search path. Once you put a file in your own personal search path, it will be always accessible. Make sure you save the search path. You will use the tools addpath, savepath, pathtool, etc.
Be careful. Do NOT put your files in the MATLAB directories that came with MATLAB itself. Even if you have write permission in those directories, do not use those places to save your files. This is important. If you do not heed my warnings, then you ill find your files will not seem to have changes implemented in them when you modify your files. You would virtually be forced to quit and restart MATLAB to see any changes. (Or you would need to learn about the toolbox cache.)
A virtue of putting your files in you own separate directories is now when you upgrade MATLAB, all you need do is make sure the search path is also set with those directories on the path, and your files will become accessible to you in the new release.
All of this is NOT the same thing as making a function built-in, in the sense that it is now part of MATLAB itself. (You don't really want that anyway.) However, your files will be as accessible as if they were built-in.

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by