dynamically change simulink library path
조회 수: 7 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2018년 9월 27일
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.
채택된 답변
추가 답변 (1개)
Fangjun Jiang
2018년 10월 11일
편집: Fangjun Jiang
2018년 10월 11일
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
bdclose()
bdclose('all')
bdIsLoaded('lib')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!