In this previous post, I was tuning the value of a Gain block. This week I received the question: Can I do the same for a parameter of type popup?
The Problem
I have this model with a masked subsystem that has a popup parameter. Under the mask, the variable associated with the popup, choice, is used to control what type of noise is added to the signal: If I try to simulate the model as described in the previous blog post, I get an error saying that it's not possible to use setBlockParameters and RapidAcceleratorUpToDateCheck='off' at the same time: in(1:3) = Simulink.SimulationInput(mdl);
in(i) = in(i).setModelParameter(SimulationMode='Rapid');
in(i) = in(i).setModelParameter(RapidAcceleratorUpToDateCheck='off');
in(1) = in(1).setBlockParameter(blk,'choice','Random');
end
ans = 'Cannot set 'BlockParameters' on SimulationInput object when 'RapidAcceleratorUpToDateCheck' is set to 'off'.'
The Solution
The first thing you need to do is associate an enumeration with the popup parameter. For this example, the enumeration is: I then associate it with the parameter in the Mask Editor by double-clicking on the Type options field: In the Subsystem under the mask, I need to modify how the variable is specified in the block dialog. For this example, instead of using the variable choice directly, I need to specify noiseValues(choice). The documentation explains the syntax, but in short you begin with the enumeration name (noise in this example) and you append "Values" to it. Then you pass to it, between parenthesis, the popup variable (choice in this example):
If you create the masked Subsystem that way, you will notice that the dialog has a new "Associate Variable" option:
This will open a dialog to specify the variable and optionally create it if it does not exist. For this example, I will create it myself:
noiseType = noise.Random;
Once you click ok, in the block dialog, you will see the variable name and the enumeration type next to it:
And that's it, now it's time to simulate the model:
mdl = 'testNoiseTunable';
in(1:3) = Simulink.SimulationInput(mdl);
in(i) = in(i).setModelParameter(SimulationMode='Rapid');
in(i) = in(i).setModelParameter(RapidAcceleratorUpToDateCheck='off');
in(1) = in(1).setVariable('noiseType',noise.Random);
in(2) = in(2).setVariable('noiseType',noise.Pulse);
in(3) = in(3).setVariable('noiseType',noise.Sequence);
out = sim(in,'ShowProgress','off');
Once the simulation completes, we can plot the results and confirm that the parameter has been tuned:
plot(out(1).yout{1}.Values);
plot(out(2).yout{1}.Values);
plot(out(3).yout{1}.Values);
legend({'Random','Pulse','Sequence'});
Now it's your turn
If you are creating masked library blocks with parameters of type popups, be nice to your users and associate the popup with an enumeration type. This will enable them to tune it in Rapid Accelerator optimally. This will also enable the parameter to be tuned if the model is deployed with Simulink Compiler.
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.