dynamically change simulink library path

26 visualizzazioni (ultimi 30 giorni)
Hello,
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
  2 Commenti
Walter Roberson
Walter Roberson il 27 Set 2018
For something like this, I would expect that it might be necessary to issue either a "clear all" or a bunch of individual clears, one for every S function used by the library, perhaps even one for every .mdl or .slx invoked. Though maybe you could use "rehash path".
When MATLAB encounters a function during execution, it parses it and caches the pre-parsed version and uses that version unless it detects at some point that a new version of the source file exists. MATLAB generally rechecks the current directory each time it returns to the command prompt, but not necessarily other directories, and sometimes it misses noticing changes. Furthermore, the pre-parsed version is where persistent variables are attached to. Sometimes it is necessary to "clear" the function or rehash to get MATLAB to drop the old parsed version.
Timo Dietz
Timo Dietz il 11 Ott 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.

Accedi per commentare.

Risposta accettata

Timo Dietz
Timo Dietz il 12 Ott 2018
Hello,
many thanks for your efforts. Meanwhile I found a workaround which does the job. It points to the direction Fangjun proposed:
close_system('MyLib.slx')
rmpath('LibPath_A')
addpath('LibPath_B')
load_system('MyLib.slx')
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

Più risposte (1)

Fangjun Jiang
Fangjun Jiang il 11 Ott 2018
Modificato: Fangjun Jiang il 11 Ott 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
bdclose()
bdclose('all')
bdIsLoaded('lib')

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by