This topic has been permanently closed and transferred to MATLAB Answers.


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

Adam on 18 Nov 2025 at 9:26

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.