run code "at import time" for example, when a class gets added to the path

30 views (last 30 days)
In python, I find it a useful feature to run some code when a module gets imported. For example, to register the import function of a class in a list of importers:
class Demo
# some stuff
def import(data : dict)-> Demo
# blabla
register_importer(Demo,import)
How should I do this in Matlab? As far as I know, there is no mechanism to run some code 'at import time' i.e. when a class gets added to the path.
Is there maybe such a mechanism in a namespace? For example: assume I have the following namespace:
+my_namespace
Demo.m
import.m
init.m
now when I add this namespace to my path, it will automatically run the init.m script to register importer.
  2 Comments
Steven Lord
Steven Lord ungefär 8 timmar ago
Can you say a little more about what you would want to do with this init.m script on importing if this functionality existed? It's possible that there is an alternate way to achieve your goal.
Also, would you want this initialization code to be run whenever the folder containing the namespace folder was added to the path, whenever you actually called import to add the namespace to the import list, whenever you cd to the namespace folder's parent folder to make it accessible, or all of the above?
Adam
Adam ungefär 13 timmar ago
Basically, I have a toolbox in a namespace, and I want to run some basic things only once when the functions and classes inside the namespace become available to the user. So basically all three options you mention***.
Things I want to do at the moment are registering saving/loading functions, setting up the environment, checking whether some software is installed, ...
I know there are many "runtime" ways to solve this problem (Rik already suggested one way). I'm wondering whether there's a mechanism to run some code "at compile time" or what I would consider to be matlab's equivalent of "at import time".
*** No idea about importing a single function from a namespace, or the weird case where someone changes directory inside running code and some namespace can become available and then gets removed while a script is running.

Sign in to comment.

Answers (1)

Rik
Rik on 19 Nov 2025 at 9:56
My personal solution is to use something like GetWritableFolder. I then load the defaults from a file in that folder. If the file/folder doesn't exist, the function didn't run before, or the output didn't persist.
It isn't quite the same as running code on import, but for my purposes import and first run are generally equivalent.
Note: to solve performance issues I generally store a success flag in a persistent variable. That doesn't persist between sessions, but it helps with repeated calls.
  2 Comments
Adam
Adam ungefär 10 timmar ago
Edited: Adam ungefär 10 timmar ago
So you would do it with a persistent variable in the constructor basically:
classdef Demo
methods
function self = Demo()
persistent is_registered
if isempty(is_registered)
register_importer(blabla)
is_registered = true
end
end
end
end
Too bad that this pollutes the actual code of the class with things that are not really related to its definition. Also, I guess a subclass can forget to call its superclass constructor and then it would break.
Rik
Rik ungefär 6 timmar ago
Essentially, yes.
I don't know if I completely agree with you on the second part. If some code needs to run on install/import, doesn't it make sense to have that code in your class defenition? It is arguably a component of your class (since it is apparently required to have run before you use it).
Also, is the risk you identify reasonable? Since the code only has to run once? That would mean that a subclass should forget to call the superclass constructor and the superclass should not have been called at all, ever. I don't mean to ask this as a rhetorical question. It might very well be a real risk in your situation.
This would also all depend on the answers you will give to Steven.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!