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

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

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 2021년 2월 8일
편집: Stephen23 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일

1 개 추천

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일
편집: Yilmaz Baris Erkan 2021년 2월 9일
Okay so I have this main folder "Works" as my Matlab path. In it, I have various studies in different folders. When I place this code in one of the folders, say "Folder 1", and when I need to use it in some other code that is in the "Folder 2", it gives me an error indicating that the code I am calling is in "Folder 1". Which makes sense.
And now, I am asking if there is a way to eliminate this issue (if making it a built-in function works, thats great).
Matt J
Matt J 2021년 2월 9일
편집: Matt J 2021년 2월 9일
Put all the mfiles that you want to have generally available (irrespective of where you are working) in their own dedicated folder or set of folders, (for example, C:\Users\Yilmaz\My mFiles). Then put all of those dedicated folders on the Matlab path using pathtool(). Save the path using savepath() if you want the path to be remembered in future Matlab sessions.
Now, you will be able to invoke those mfiles and Matlab will be able to see them no matter what other folders you happen to be working in.
Stephen23
Stephen23 2021년 2월 9일
편집: Stephen23 2021년 2월 9일
"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.
@Matt J thank you for the guide and explanation. I appreciate it.

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2021년 2월 8일

1 개 추천

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일

1 개 추천

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.

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품

릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by