Using multiple instances of a referenced model with different port dimensions

Hello,
I have multiple Simulink models that contain multiple instances of the same subsystems. Whenever I want to make a change to the functionality of such a subsystem, I need to copy-paste the new subsystem in several different larger models to ensure that they are all up-to-date. This workflow is sub-optimal for obvious reasons.
I am very much charmed by the concept of using referenced models. In this case, I would just need to alter one .slx model, and that would automatically be used in all of my other models. However, it appears that Simulink struggles with using inherit port dimensions in such a referenced model. I would like to be able to use multiple instances of the same reference model for a 1-dimensional input as for a n-dimensional input, but I can only seem to hard-code the port dimension into the model itself.
As an example, I made a simple model where I multiply the input by 10. The port dimension of port x is set to '-1' for inherit:
This model is saved as 'example_10X.slx' and used in another model as a referenced model. Using an input of size [1x1] works without issue:
However, when I change the input to a different size, Simulink gives an error:
Error in port widths or dimensions. 'Input Port 1' of 'main/Model1' is a one dimensional vector with 1 elements.
This error can be fixed by hard-coding the input of port x to be [1x2], but than I can no longer use the same model for the 1-dimensionsal situation. Besided, I would definitely prefer that Simulink inheritly determines the size of the signal before a simulation run.
I tried using the "create variable..." function in the signal attributes of the port and adding the created variable to the block parameters (it is named signalSize), but that gives an error as well when I try to run it.
Invalid setting in 'example_10X/x' for parameter 'PortDimensions'.
Caused by:
  • Parameter 'PortDimensions' of 'example_10X/x' is non-tunable but refers to model arguments (signalSize)
I tried updating to the latest Matlab prerelease as well (2024a at the moment of writing), but that did not change anything.

4 Comments

Seems like there is no way to do what you want using a Referenced Model AFAIK. I would rather suggest you to make the model as a Subsystem and convert into a library. So you can use it as much as you want.
Thanks for taking the time to answer. I could also not find any way to do this, that is why I asked. The thing with library models is that whenever you make a change to the library component, you have to manually update all of the instances where you use them components. At least that is my experience from several releases ago, and the help page you linked also mentions that you have to update your linked instances in that case.
For now I have found that Referenced Subsystems function more in a way that I would like. These do accept differences in inputs and outputs, so I am pursuing that route for now. Referenced subsystems don't have a model workspace, so it is slightly more challenging to set them up in a parameterized way. Using a simple mask to change some parameters seems to work normally though. Also, I guess you don't get the memory benefits of a referenced model when using a subsystem instead.
Yes, I agree. I thought about the mask method too, why not use it then? I really cannot find a way to vary the sizes for inports at the moment. I also found Matlab Answer Thread where it mentions it’s not possible for Referenced models, but can’t find it now.
I think we have run into the same Matlab Answer Thread. From what I have learned so far, it indeed seems impossible at the time. The mask option for Referenced Models enables the possibility to have instance specific parameters, but it does not work for the port dimensions. I think it has something to do with the compilation of the model, where Simulink wants to reuse the exact same model code for the different instances.
The masks for Referenced Subsystems have been very useful since it enables instance specific parameters even while there is no model workspace for each of the subsystems. So for now, I am using this option.

Sign in to comment.

Answers (1)

Hi Robin,
As I can understand, you are facing issues when trying to send signals of varying sizes to a referenced model from different parent models. Currently, there is no method to inherit the size from the parent model to the inport of the referenced model in Simulink.
However, here is a workaround that you might find useful:
  1. Declare a variable in the base workspace: Define a variable, say "signalSize", that will store the size of the signal. If you want to avoid declaring the variable manually in MATLAB each time you change the model, you can use model callbacks. For this, right click on Simulink canvas, then select "Model Properties", then go to "Callbacks" and define the variable in the "InitFcn" callback.
  2. Access the variable in inport: Use the declared variable in Block Parameters of the inport block. Change "Port Dimensions" to "signalSize".
If you define using the "InitFcn" callback, the variable "signalSize" will be updated everytime the parent model is initiated and the referenced model will use the updated value from the parent model.
Hope this helps!

4 Comments

Hi Yash,
These are indeed useful workaround in typical cases where you would use the referenced models in different parent models. I did try this as well with succes. However, a problem occurs when you have a situation in which the parent model can contain multiple instances of the same referenced model, but with different dimensions for these different instances, which is what I would like. The issue is that when you use the variable "signalSize" it automatically applies to all instances of this referenced model. Idealy, I would like to have this variable in the model workspace, but that does not seem to be possible as it says that it is "non-tunable".
For my case, Referenced Subsystems seem to be the better option as they inheritly decide which signal size to use, and it does not need to be the same for each instance in the parent model.
Robin
Hi Yash,
This could work, but only if one top model contains just one 'instance' of the referenced model, or if all instances use the exact same signal size. In the end there is only one parameter signalSize in the base WS (and hence, with one value) that all instances refer too.
I encountered this problem in the past. I tried to add signalSize to a model mask or as model argument, but that implicitly makes the parameter a tunable Simulink.Parameter, which is not supported for port specifications (since those need to be fixed during runtime). This is the reason behind the error Robin copied in before. Instead, for now I initialize the signal size for the maximum value in the model hierarchy, and buffer with 0s when needed.
In the end model references are mostly nice when you generate code, or when the model requires standalone behaviour (check this handy flowchart). Referenced subsystems could indeed be a great solution if you do not really care about those things.
Hi Robin and Timo,
You are correct that it would only work if there is a single instance of the model reference block or all instances use the same signal dimensions.
The best alternative for your case is the Subsystem Reference block as it inherits the input size from the parent model. Also, you don't have to update model everywhere as it takes reference from the same file.
Model mask and model argument won't work in your case as the port dimensions are non-tunable. Padding signal with zeros could be a possible approach but it will take large amount of memory which isn't required. Subsystem Reference would be a better approach.
The documentation of the Subsystem Reference is as follows: https://www.mathworks.com/help/simulink/ug/referenced-subsystem-1.html
Hi,
I've recently discovered that this particular behavior is not a bug, it is actually expected and is consistent with what is described in the official documentation. Refer to the following link for the documentation

Sign in to comment.

Categories

Find more on Create Large-Scale Model Components in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 8 Jan 2024

Commented:

on 19 Jan 2024

Community Treasure Hunt

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

Start Hunting!