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:
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.
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 で Subsystems についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!