bio_img_simulink

Guy on Simulink

Simulink & Model-Based Design

How to programmatically configure a Simulink simulation?

Here is another frequently viewed MATLAB Answers post:
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);
Let's check what's the original value using get_param, change it, and confirm that it has changed:
get_param(mdl,'SolverName')
ans = 'VariableStepAuto'
set_param(mdl,'SolverName','ode45')
get_param(mdl,'SolverName')
ans = 'ode45'
The same works for blocks:
blk = 'MyTestModel/MyLUT'; %The path of the block
get_param(blk,'BreakpointsSpecification')
ans = 'Explicit values'
set_param(blk,'BreakpointsSpecification','Even spacing');
get_param(blk,'BreakpointsSpecification')
ans = 'Even spacing'
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.

|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.