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

2 Ansichten (letzte 30 Tage)
Yilmaz Baris Erkan
Yilmaz Baris Erkan am 8 Feb. 2021
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 Kommentare
Matt J
Matt J am 8 Feb. 2021
What do you mean by "including the .m file wherever I need"? No matter what the implementation of the function is, it still has to be placed on the Matlab path so that Matlab can see it.
Stephen23
Stephen23 am 8 Feb. 2021
Bearbeitet: Stephen23 am 8 Feb. 2021
Put it on the MATLAB path (but definitely NOT in any installation folder or any application):

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 8 Feb. 2021
Bearbeitet: Matt J am 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 Kommentare
Stephen23
Stephen23 am 9 Feb. 2021
Bearbeitet: Stephen23 am 9 Feb. 2021
"I am asking if there is a way to eliminate this issue"
So far four people have told you to use the MATLAB Search Path, which you can define to include those folders. Have you tried doing that?
Note that in general it is better if data folders are not on the MATLAB Search Path, and you should read/write data files using absolute/relative filenames.
"(if making it a built-in function works, thats great)."
That is not the solution.
Yilmaz Baris Erkan
Yilmaz Baris Erkan am 9 Feb. 2021
@Matt J thank you for the guide and explanation. I appreciate it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Fangjun Jiang
Fangjun Jiang am 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 am 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.

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by