MATLAB Answers

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

44 views (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 8 Feb 2021
Edited: Matt J on 8 Feb 2021
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 Comments
Yilmaz Baris Erkan
Yilmaz Baris Erkan on 9 Feb 2021
@Matt J thank you for the guide and explanation. I appreciate it.

Sign in to comment.

More Answers (2)

Fangjun Jiang
Fangjun Jiang on 8 Feb 2021
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 on 9 Feb 2021
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.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!