Using multiple instances of a referenced model with different port dimensions
27 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585561/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585566/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585571/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585576/image.png)
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 个评论
madhan ravi
2024-1-12
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.
回答(1 个)
Yash
2024-1-15
编辑:Yash
2024-1-16
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:
- 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.
- 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 个评论
Yash
2024-1-16
编辑:Yash
2024-1-16
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
2024-1-19
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Component-Based Modeling 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!