dynamically change simulink library path

18 views (last 30 days)
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
Timo Dietz
Timo Dietz on 11 Oct 2018
Many thanks for your answer. Unfortunately 'rehash' and/or 'clear' does not work. Even though I close and re-open the model file Matlab sticks to the library once found - independent of the current search path. Closing Matlab completely is no option.

Sign in to comment.

Accepted Answer

Timo Dietz
Timo Dietz on 12 Oct 2018
many thanks for your efforts. Meanwhile I found a workaround which does the job. It points to the direction Fangjun proposed:
The advantage is that you do not have to close/open the Simulink model. The blocks are updated after a couple of seconds automatically - at least by saving the model or descending a block.
Again, thanks to all of you for your feedback. best regards Timo

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 11 Oct 2018
Edited: Fangjun Jiang on 11 Oct 2018
I did an experiment and it seemed to work.
  1. Construct a library model with a SubSystem block, mask it as 'LibA' and save it as "lib.slx" in folder .\FoldA
  2. Mask the SubSystem block as 'LibB' and save it as "lib.slx" in folder .\FoldB
  3. addpath('.\FoldA'), construct a model, drag and drop the 'LibA' block. save it and close the model.
  4. 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!

Translated by