How to run a .m script in the same folder as the Simulink model using Model Callbacks?

18 views Asked by At

Here is a model mdl/mdl.slx and its init script mdl/mdl_init.m. mdl/mdl_init.m should run as the model's preload callback. To make it foolproof, it's better that user do not need to add mdl/ to path to open the model. The user only needs to drag the model file to command window and all paths are added by mdl/mdl_init.m.

The problem is MATLAB do not recognize the current folder of the Simulink model. Using mdl_init or run("./mdl_init.m") in the preload callback doesn't work. run("./mdl_init.m") works if it's called by another .m script without adding path, so there may be some way to do that in Simulink.

On the other hand, since run accepts absolute path, getting the current Simulink model file/folder is enough to locate the script file. How to do that?

0

There are 0 answers