Why does the Simulink.V​ariantExpr​ession object in R2023b does not seem to be compatible with the Simulink.Variant object in R2017b

1 view (last 30 days)
Our company uses the same Simulink model in MATLAB R2017b and R2023b which contains variant subsystems. Unfortunatley we need to continue using our R2017b due third party contractors.
The way the variants were used within R2017b and in R2023a was by Simulink.Variant object and defined in the base workspace. Variant controls are switched using conditional expressions.
Typing the following into the command window in either R2017b or R2023a creates the variant variable as expected.
variantTest1 = Simulink.Variant('config1' || 'config2')
variantTest1 =
Simulink.Variant
Condition: 'config1' || 'config2'
When variables were created in the workspace as shown below, they were then saved to .mat file which was added to the project, and set to open on startup.
config1 1x1 Variant
config2 1x1 Variant
However, recently I had to add another variant subsystem in R2023b, which I did the same was as usual, however it was now a variantExpression instead of being Simulink.Variant as per below.
variantTest2 = Simulink.Variant('config3' || 'config4')
variantTest2 =
VariantExpression with properties:
Condition: 'config3' || 'config4'
This didn't bother me at the time because I could compile the model fine and it appeared very similar in the workspace as 1x1 VariantExpression, however when a colleague using R2017b loaded the model they noticed that the
workspace variable was now a 6x 1 Vector with the elements being of double data type as shown below.
config3 [3707764736;2;1;1;1;1]
config4 [3707764736;2;1;1;2;2]
I have since seen that the Simulink.VariantExpression object class has been introduced in R2023b which is why I have not seen this issue before, however why is this not backwards compatible with R2017b when creating and using variants under the same context?
I noticed while viewing the block parameters GUI within Simulink that there are two variant drop down options for control mode and activation time. I have left these and not changed them and they are currently set as expression and update diagram respectively.
Could I please receive some guidance as to how I can resolve this problem programatically if possible?
Currently, the work around is to delete and re-create the variant variable in the workspace in R2017b, however it would be much more preferrable to not have to do that.
Regards,
Dan

Accepted Answer

Dinesh
Dinesh on 10 Apr 2024
Hi Dan,
The new class "Simulink.VariantExpression" is not recognized in R2017b. Hence, when you try to load the model in R2017b, I believe that MATLAB falls back to loading the raw data that makes up the object. This raw data is likely represented as 6x1 vector of doubles.
Instead of creating these objects in MATLAB command window, I would recommend using the model callbacks. In your case, you can use the "PreLoadFcn" callback and write the code to create the "Simulink.Variant" object within this callback. In R2023b, you can find this option by right clicking on the Simulink canvas and then choosing the "Model Properties" option. In the "Model Properties" pop-up, go to the "Callbacks" tab and click on "PreLoadFcn". The relevant code can be entered in the code space after which you can apply the changes. Here's an example:
variantTest1 = Simulink.Variant("A==1");
The code defined in this function is called every time before the model is loaded. A new "Simulink.Variant" object will be created in both R2017b and R2023b. The object created in R2023b will not be used in R2017b if you use this approach.
Here's the documentation link for model callbacks: https://www.mathworks.com/help/simulink/ug/model-callbacks.html
I hope this helps.

More Answers (0)

Categories

Find more on Programmatic Model Editing 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!