Search code examples
functionmatlabpath

How to tell Matlab to "reload" the functions that exist in a folder in my path?


I keep many homemade functions in a common directory like this: U:\matlab\functions (U: is a mapped network drive in Windows 10).

In my startup.m I add it to my path:

addpath('U:\matlab\functions');

I can access functions in that directory with no problems until I add a new function in that directory during a session. After I add a new function and try to use it, Matlab doesn't find it. I try to use the new function and it says

Unrecognized function or variable 'newfunction'

Or I do a

which newfunction

and it says

'newfunction' not found

I have to close and restart the Matlab session to have it find the new function I just created. Is there any way to tell Matlab to "reload" what it sees in the functions directory that is included in my path without restarting my session?

I'm using Matlab R2022a, but I've seen this in earlier versions.


Solution

  • Yes, you can use rehash:

    rehash updates the MATLAB® list of known files and classes for folders on the search path that are not in matlabroot. It compares the timestamps for loaded functions against their timestamps on disk. It clears loaded functions if the files on disk are newer.