Using multiple instances of a referenced model with different port dimensions

21 views (last 30 days)
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
madhan ravi
madhan ravi on 12 Jan 2024
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.
Robin Mommers
Robin Mommers on 15 Jan 2024
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)

Yash
Yash on 15 Jan 2024
Edited: Yash on 16 Jan 2024
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
Yash
Yash on 16 Jan 2024
Edited: Yash on 16 Jan 2024
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
Yash
Yash on 19 Jan 2024
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 Component-Based Modeling in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!