How to programmatically configure a Simulink simulation?
While this question asks specifically about stop time and solver step size, I think this can be generalized to this question: How to find the programmatic equivalent of a block or model setting in Simulink?
My answer is: Use What's This?
Model Parameters
In case you never noticed, you can right-click on most configset parameters to see a "What's This?" menu being offered:

Clicking it opens a short description of the setting and the programmatic equivalent:

Clicking the Show more information link opens a separate What's This Help window with more details:

If you scroll toward the bottom, you will find a table listing everything you need to know about the programmatic use for this setting:

Block Parameters
Most blocks parameters also have a What's This?:


Now that we know how to obtain parameter names and possible values, let's see how to apply them.
Modifying the model
If you want to modify the model with the new setting value, use set_param. For the settings described above, this looks like:
mdl = 'MyTestModel';% The name of the SLX file
open_system(mdl);

get_param(mdl,'SolverName')
set_param(mdl,'SolverName','ode45')
get_param(mdl,'SolverName')
The same works for blocks:
blk = 'MyTestModel/MyLUT'; %The path of the block
get_param(blk,'BreakpointsSpecification')
set_param(blk,'BreakpointsSpecification','Even spacing');
get_param(blk,'BreakpointsSpecification')
At this point, you can save your changes using save_system
save_system(mdl);
Simulating with different parameter values
In some cases, you want to simulate the model with different parameter values, but not dirty or modify the model file. For that, I recommend using a Simulink.SimulationInput object. Here is an example where I simulate a model with 3 different solvers:
mdl = 'testStep';
open_system(mdl);

solverChoices = {'ode1be','ode3','ode5'};
N = length(solverChoices);
% Creating an array of Simulink.Simulationinput objects
in = repmat(Simulink.SimulationInput(mdl),N,1);
% specify different solvers for each simulation
in = arrayfun(@(i) in(i).setModelParameter('SolverName',solverChoices{i}),1:N);
out = sim(in,'ShowProgress','off');
% Plot results
figure; hold on;
arrayfun(@(i) plot(out(i).yout{1}.Values),1:N);
legend(solverChoices)
Now it's your turn
How do you find the programmatic way to configure Simulink? Let us know in the comments below.
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。