Tuesday, June 25, 2024
HomeMatlabUnifying MATLAB and Simulink: A Person Story Half 3 » Man on...

Unifying MATLAB and Simulink: A Person Story Half 3 » Man on Simulink

In at present’s submit, I’ll add options to the framework introduced within the earlier submit to assist controlling variants.

In case you missed the earlier posts on this sequence, listed here are hyperlinks to all posts:

Variant slPart Block

To deal with variants, I added a second template block to the library the place I had beforehand saved the slPart template block. This second template is a Variant Subsystem with the identical masks and identical masks initialization callback as described in my earlier submit:
Utilizing label mode will permit us within the subsequent step to vary the lively variant primarily based on the category of the slPart object specified within the masks parameter.

When I’ll copy this block to fashions, I will add to it as many algorithm variants as I would like.

Including Variant Specification to slPart

The objective right here is to pick the lively variant primarily based on the category of the article, which is specified as a masks parameter. For that, I add the next code to the maskInit methodology of slPart:

classdef slPart < deal with

properties (Hidden = true)

BlockPath char



operate obj = maskInit(obj,blk)

obj.BlockPath = blk;

% If the block has variants, set lively variant primarily based on the article class

if strcmp(get_param(blk,‘Variant’),‘on’)

variantChoices = get_param(blk,‘VariantChoices’); % Get all variant selections

variantChoices = {variantChoices.Title}’; % Extract the labels

classList = [class(obj);superclasses(obj)]; % Get the listing of sophistication and superclasses

matchingChoices = intersect(classList,variantChoices); % Discover the label that matches the category or closest superclass

set_param(blk,‘LabelModeActiveChoice’,matchingChoices{1}); % Apply the primary match





This code will examine the variant management labels of every variant and discover the one which matches the category of the article or its closest superclass. On the finish, it makes use of set_param to activate the chosen variant primarily based on its label (Extra on that under).

Implementing a Variant slPart

Let’s hold constructing on the spring instance from the earlier submit. I add my template block to a mannequin and create two variants, one is similar linear spring as within the earlier instance, and the opposite is a non-linear model:

I create a brand new slPart class for the non-linear model and put it aside within the +springLib package deal created within the earlier submit. Any more, I’ll seek advice from this class as springLib.springNonLinear:

classdef springNonLinear < slPart









operate obj = springNonLinear()

obj.m = 1;

obj.displacementBreakPoints = -5:5;

obj.stiffnessTable = tanh(-5:5);

obj.velocityBreakPoints = -5:5;

obj.dampingTable = 2*tanh(-5:5);




Now, the important thing factor for the masks initialization methodology to have the ability to set the right variant is to set the label of every variant to the identify of corresponding class.

One factor I need to level out right here is that it will be attainable to acquire an identical variant activation logic utilizing expression mode as an alternative of label mode. The primary distinction in my view is that, by utilizing label mode managed by the masks initialization methodology outlined within the slPart superclass, the complexity is managed in a single place: within the slPart superclass. Whereas expression mode provides extra prospects, for instance totally different activation occasions, it places the complexity on writing down the situation expression on every person including a brand new variant.

Extra Variant Parameterizations

As soon as the Variant Subsystem has been configured, as described in submit 2 of this sequence, I can optionally create variant parameterizations for the newly created non-linear spring:

With these saved within the +springLib package deal folder, I now have doubled the scale of my library of springs, which I can nonetheless entry by tab-completion:

Choosing a Half and Simulating the Corresponding Variant

As soon as the mannequin is correctly configured and the MATLAB lessons have been created, I can assign an object of the specified kind to the variable specified within the block masks dialog and Simulink will routinely use the suitable variant subsystem throughout simulation.

I like the truth that, if I give this mannequin to somebody, all that individual must do is to select a spring from the springLib package deal. No have to know or care about the truth that it is likely to be activating a distinct variant subsystem beneath the hood.

That method, you inherit a element the place the inside mechanics have been abstracted so you possibly can concentrate on utilizing it and never be distracted by implementation particulars.

Now it is your flip

Clearly, what I confirmed right here is the naked minimal to get this system working. For a sensible challenge, I might add validation code and error checking to higher information the person in case one thing goes fallacious.

If you wish to analyze this challenge in additional element, you possibly can obtain the present model right here.

What do you consider this method to variants? Did you or your organization got here up with different methods to systematically management variants? I might have an interest to listen to how the usability compares with what I described on this submit.

Tell us within the feedback under.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments