I am writing a Matlab toolbox and I want to do some initialization at Matlab startup. Is there any script or function that is called during the startup phase, when the toolbox directory is on the Matlab path?
I know that there is a "startup.m", but this is user-specific. And if the user has his own "startup.m" higher on the MATLAB path than the toolbox's it won't be called.
There is a "sl_customization.m" which is called whenever Simulink starts, even if there are multiple files found on the path. Maybe there is something similar for Matlab itself?
EDIT
Maybe it's too early for this. But from your answers and comments I see, that there seems no such mechanism built-in by default.
Only to clarify: I don't want to change the user's settings in any ways. The toolbox can be extended by using plugins. During startup I want to detect such plugins and temporarily (i.e. only for the current session) add their base-directory on the MATLAB path if they have their own documentation available. So that it can be found in the Help Center.
Would it make sense to change the matlabrc in a way that all "startup.m" files on the Matlab path are executed one after the other (best from the lowest on the path to the highest, so that the one highest would be able to overwrite the other's settings)? Then I could ship my toolbox with a "startup.m" which is executed at startup and even if the toolbox is deleted (and not correctly uninstalled) it would not harm the Matlab environment as by default there would only be one startup file. Or would this go too far? Is there any problem with this approach I have not thought of?
When MATLAB starts up, it runs
matlabrc.m
, which is not user-specific and is stored in the\toolbox\local
folder.This file contains lots of stuff that runs at startup, and it's intended that system administrators should modify it before installing MATLAB, to implement startup options that they want all their users to implement. It's
matlabrc.m
that actually makes the call to the user'sstartup.m
, if it exists. Typedoc matlabrc
to find out more, and typeedit matlabrc
to read yours and see what it does.So you could in theory modify that. However, there are a few issues with that:
matlabrc
runs before many MATLAB services have started, so some things that you might like to do won't be possible.matlabrc
being called.In addition, as mentioned in a comment, it will very likely annoy a lot of users, who don't want your startup settings and won't find it easy to remove them.
I would suggest you create a script (let's say
mytoolboxstartup.m
), and then as part of your install process you provide the user with a desktop shortcut (or something similar) that will runmatlab -r mytoolboxstartup.m
.In this way, the user can choose to start either a regular MATLAB, or a MATLAB that has been set up in the way you recommend for your toolbox. In addition, if users want to write their own functions calling your toolbox, they can call
mytoolboxstartup
directly for themselves.