dynamically change simulink library path
20 views (last 30 days)
Show older comments
Timo Dietz on 27 Sep 2018
Answered: Timo Dietz on 12 Oct 2018
without changing the Simulink model I'd like to switch between two source libraries for the blocks the model contains.
E.g. there is an old Simulink lib stored in folder A and a new Simulink lib - with the same name - in folder B. Now I'd like to control which one is taken by the model.
First I simply changed the search path - including folder A and removing the unwanted path B. This worked in principle but Simulink somehow remembers path B and still uses that one.
Is there a way to unload/refresh Simulink lib associations? Any other idea how to realize something like this?
Many thanks in advance
More Answers (1)
Fangjun Jiang on 11 Oct 2018
Edited: Fangjun Jiang on 11 Oct 2018
I did an experiment and it seemed to work.
- Construct a library model with a SubSystem block, mask it as 'LibA' and save it as "lib.slx" in folder .\FoldA
- Mask the SubSystem block as 'LibB' and save it as "lib.slx" in folder .\FoldB
- addpath('.\FoldA'), construct a model, drag and drop the 'LibA' block. save it and close the model.
- rmpath('.\FoldA'); addpath('.\FoldB'); open the model, it shows 'LibB'.
This is what I expected and it seemed that way. As long as you close the model and re-open the model, the library file used should be the first found in the path. Don't just use a simple block such as a Gain block, having gain of 1 in .\FoldA\lib.slx and gain of 2 in .\FoldB\lib.slx. The resulting model still points to the Gain block in the built-in Simulink library and the Gain value difference is regarded as different parameter values.
If the model is not closed and re-opened, the change of the library search path didn't seem to have effect, even if I tried to update the model.
A complicated situation could be that the library was used by multiple open models. In the above example, if a second model which also uses the block from lib.slx is kept open, then in step 4, the block still shows 'LibA'.
So the key point is whether the library has been cleared/removed from the memory. In a typical use case, close the model, switch the library search path, re-open the model should be able to switch the library. But to be robust, you really need to make sure to close all model and library files, or check if the library file is still loaded in the memory. Use these functions accordingly
Find more on Interactive Model Editing 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!