MATLAB: Does the Simulink model incorrectly access a library with the same name as the referenced library in Simulink

clearliblinksmdlrefreshrehashrepeatsimulinkunlink

I have two separate library files with the same name in different directories. I also have two separate models in these directories, and each contains reference blocks to the library in their directory. I start Simulink and run one model. After that simulation is complete, I attempt to run the second model. However, when I do this, Simulink accesses the wrong library. It appears to think that the reference blocks in my second model are linked to the blocks in the first library. This occurs even though the first library is no longer on the MATLAB path.

Best Answer

The ability to reload all referenced libraries used by a particular model is not available in Simulink. This shortcoming can cause a reference block to link to the wrong library in certain cases.
To work around this issue, use the CLOSE_SYSTEM and FIND_SYSTEM functions to close all libraries before opening a model. For instance, take the case where two models both link to blocks in two separate libraries that have the same name. When the first model is run, the library needed to run the model is loaded invisibly (there is no Simulink window displaying the loaded library). When that model is closed, the library that was invisibly loaded is not automatically closed. This causes a problem when the second model is run. Simulink will usually look on the MATLAB path to find the library referenced in the second model. However, since a library of the given name is already open, then Simulink will use that library before looking on the path. Thus, in this case, Simulink will not reload the referenced library and therefore the wrong library will be referenced. This can be done with a command such as the following:
close_system(find_system('type', 'block_diagram','BlockDiagramType','library'))