MATLAB: Creating a built in function

built-in-functionMATLAB

I'm sure literally everyone who doesn't know a solution to this has done exactly what I'm currently doing…
I've got a special folder on my desktop called 'Functions' where I store all my .m function files that I've written over the years and use quite often. In order to use them in various workspaces/codes I have "Change the MATLAB current folder or add its folder to the MATLAB path." Which to be fair is an acceptable solution, but I would like something a little more elegant since I often update the code to these functions but it doesn't update every copied iteration of this function.
What I would like to do is add/edit the built in functions for MATLAB so I don't have to change the workspace folder or add the function to the path! I was hoping it would be as simple as adding a folder to the C:/Program Files/MATLAB/R2020a/toolbox/matlab and adding all my functions there but I've had no such luck as it still asks me to "Change the MATLAB current folder or add its folder to the MATLAB path."
Has anyone figured out how to do this or maybe another solution? I've searched high and low in the Community Forums and Google and figured it was about time to make a post about it.

Best Answer

Don't put it on the Desktop, create a folder in your user area for MATLAB and then add that folder to the MATLABPATH and make permanent.
Then, do NOT make copies of these functions all over the place, you'll reference the one and only version (excepting of course, if you do have copies for various classes of inputs; there's a whole hierarchy of directory structure outlined for that purpose.
Secondly, don't even think of adding to or modifying TMW-supplied functions -- if you do that, as soon as you update you've got to do it all over again besides the confusion that ensues from having done.
If you are working on a specific project, create a working directory specifically for it and a script to move to/from that subdir and perhaps do some other housekeeping on the way...
My prototype for this purpose looks like
>> type PROJECT
function PROJECT(varargin)
% Set environment for given project work environment
% PROJ -- set working directory for PROJ data work
% Optional arguments
% 'quit' -- restore environment to normal working
% directory prior to initial invocation
% 'init' -- clear local persistent working directory
% from present value for clean slate
% 'info' -- display current target data working directory
% and present return working directory on 'QUIT'
persistent workpath
p='C:\Set\Desired\Path\String\Here';
% handle optional arguments, then quit
option=[varargin{:}];
if strncmpi(option,'init',3), clear workpath, return, end
if strncmpi(option,'info',3),
fprintf('PROJ data location: %s\nFormer working dir: %s\n',p,workpath)
return
end
if strncmpi(option,'quit',2), cd(workpath),return,end % restore working path
% save called-from directory if 1st invocation after 'init'
if isempty(workpath), workpath=cd; end
cd(p) % go to working directory
I've not got fancy-enough to make it self-modifying; I have to go in and create a new one for each new project by replacing PROJECT/PROJ with appropriate names...I do generally use all caps for these and some name that is easily associated with the given project.
Between a base \work directory for run-of-the-mill sandbox, the \UTILS directory that contains my set of functions similar to yours described above, and the above for projects, I've yet to see a message about "change MATLAB folder..."
That's been over 20 years now and counting... :)