Creating a built in function?
显示 更早的评论
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.
采纳的回答
更多回答(2 个)
Steven Lord
2020-7-29
2 个投票
You cannot add a built-in function to MATLAB like we at MathWorks can. But you can make the MATLAB scripts, functions, and/or classes that you write accessible to MATLAB by default if you want. See the documentation page for the userpath function.
Whatever you do, I strongly recommend that you not modify any of the folders located under matlabroot and that you do not add a new folder under matlabroot, at least not unless MathWorks Support instructs you to do so or you're installing a new product or an update release.
3 个评论
I built the above so long ago and just followed previous practice I wasn't even aware userpath had been introduced, Steven! :)
That makes the above easier to implement. I see it came in R2008a; in checking I see persistent came along in R2006; even before that I used a disk file to hold the working dir to do the same thing functionally...
It would have been back in mid-90s I came up w/ the above strategem.
I don't remember when TMW changed the install to create the working area in the USER folder; my recollection is that "way back when" it just used a location under the install location. Under Windows I always installed at a root level instead of the default MS penchant under "Program Files" so could have a short; easily remembered path name and particularly, get rid of the names with embedded blanks.
Steven Lord
2020-7-29
persistent is older than release R2006a. [It was "Introduced before R2006a".] It's been around since at least MATLAB 5.2, release R10 (the earliest version I chose to check) and is probably older than that.
I believe you are correct that we used to create a work folder under matlabroot for users to use, but I think we realized that keeping user files there meant they might be forgotten when users install a new release and uninstall the old. Plus at least Windows tends to be protective of people writing to the Program Files areas where MATLAB is installed by default.
dpb
2020-7-29
That's undoutedly so; didn't really mean to imply it had to have been then that started using persistent.
I couldn't begin to reconstruct the initial pass at the above -- I started w/ the original 3.1 Windows release and have had an install virtually continuously since.
Like many, very early on I also had a penchant to mung on TMW-supplied functions to either fix bugs or modify behavior to fit personal desires before learning that wasn't a wise way to go... :)
It was solving that problem of updating that got me started with installing upgrades into clean, version-named subdirectories which had the side benefit of having multiple versions available with the non-expiring license (other than maintenance). All of those early releases won't run now on 64-bit machine/OS, of course, but still have one older machine with a couple pretty early releases altho I finally did trash the old AST machine w/ NT and OS2 earlier this summer. Was a painful decision to come to... :)
I do recall for a few install cycles having to go and remember to make those changes to that few distribution files; by now I've forgotten just which were the key ones. I do sorta' recall there being a release by which what were bugs had been fixed and building a custom version of the one or two that preferred to operate differently that did put in the UTILS directory instead. I think even those had been relegated to the bit bucket by about R2013 or so. Somewhere there's an archive file but it's on a spare drive so not handy to refer back to, unfortunately.
Morgan Blankenship
2020-7-30
5 个评论
Morgan Blankenship
2020-7-30
dpb
2020-7-30
Don't understand the problem, precisely.
persistent will save the variable content between invocations so you'll have whatever was in the variable. I use just the one location but it could be an array or whatever...even a compound structure or whatever you need it to be.
Only if you need something other than keeping the info from the initial invocation that creates the new workspace until you destroy it would you need some other type of mechanism it seems to me.
Then again, I didn't try to read your function, either... :)
dpb
2020-7-31
"... to update the UTILS path for every Project."
I think there's the flaw. The UTILS location should be static and in MATLABPATH universally; there should be no need to ever update it; certainly no need to reference to get it added to a PROJECT location.
That's the point of \UTILS -- those are things you want no matter where current working directory is just as if it were a part of the TMW-distributed toolset.
I put it first so if I ever do want to refine/modify a TMW function (don't do that any more, used to be fairly frequent before a lot of the additions to base functions to add flexibility) I can alias them there and get my version first bering earlier in the search path order.
It's only stuff that is unique to the specific project that should need setting in going to the new project directory...
Morgan Blankenship
2020-7-31
dpb
2020-7-31
You simply name your function the same and put it first in the search path.
I do NOT recommend doing this; create your functions with unique names so you don't break MATLAB compatability. Aliasing builtin or supplied functions can wreak havoc on MATLAB itself if it also relies on one of those functions for a particular behavior.
It's an easy trap to fall into when first introduced to the concept of open source of the TMW m-files or that functions alias anything and everything silently, but as the old saw on some ancient maps says "There be dragons!"
Sorry I mentioned it... :)
But, I do stand by the other advice that the UTILS directory should be static and in MATLABPATH as I illustrated above; only the variable working locations should be the modified paths that use specific locations that don't need/shouldn't have global accessibility.
IF TMW would introduce namespaces it could help a lot and also aid some in the name pollution issue the proliferation of new toolboxes and the duplicated functionality of all the similar but slightly different versions of the same thing that have exploded last few years/release cycles.
类别
在 帮助中心 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!