MATLAB Answers

Run sim command with configset from within a ML-function

34 views (last 30 days)
Chris
Chris on 18 Oct 2016
Edited: Chris on 19 Oct 2016
I want to run a simulation in Simulink with sim() command from within a function. I understand that I need to assign the initial values to the model workspace. Matlab's help on sim-command) suggests that I can do this with
sim( model, 'SrcWorkspace', 'current')
Now my problem is that I need to run the simulation model with a user defined ConfigSet. As described in the help on the sim-command, the syntax to call sim with a configset is
sim(model, ConfigSet)
Calling sim with both, ConfigSet and Parameter/Value pair, results in an error.
sim( modelname, mySLConfigSet , 'SrcWorkspace','current')
"TIMESPAN" parameter must be a real scalar or vector
sim( modelname , 'SrcWorkspace','current', mySLConfigSet)
The input arguments of a single output sim command should be model name followed by either a structure with valid parameters as fields with corresponding values or a set of parameter name and
parameter value pairs
The simulation works fine, when I call sim from a script.
Now my question: Is there an easy way to combine both, i.e. run a simulink model with sim() command and with user defined ConfigSet from within a matlab function, i.e. with a user defined source workspace?
(I use Matlab R2013b with an slx-model)

  0 Comments

Sign in to comment.

Accepted Answer

Swarooph
Swarooph on 18 Oct 2016
Edited: Swarooph on 18 Oct 2016
OK, you asked for an easy way. Depending on how you look at the following, it maybe easy or not :)
It looks like sim itself does not allow for the 2 approaches to be combined. So the way to do this is to use configuration parameters set specific functions such as getActiveConfigSet and setActiveConfigSet. Following is an example function to show how to do this:
function runFooModel(mdl_name)
% Input mdl_name is a string
%Get current config set of the model
configSet = getActiveConfigSet(mdl_name);
% Create a copy of the set to manipulate it. Alternatively load one
% from an existing file
configSetCopy = copy(configSet);
% Make a change. Again, if this is another config set, this step is not
% necessary since the object will have the change built in
configSetCopy.set_param('StopTime','50');
% Set the new config set as active
setActiveConfigSet(mdl_name,configSetCopy)
% Sim with the new config set
sim(mdl_name,'SrcWorkspace','current');
% Optionally revert back to the original model config set to
% restore status
setActiveConfigSet(mdl_name,configSet)
end
Also note that based on the documentation for sim, The sim command accepts all simulation parameters as name-value pair arguments. See Model Parameters for a list of all simulation parameters. In addition, the sim command accepts a list of parameters that is sim command specific. However it looks like sim does not allow for the 2 approaches to be combined. Some of these parameters could be in the configuration parameters set so we could work around that way and call sim with only the necessary configuration parameter set. However unfortunately, SrcWorkspace doesn't seem to be a parameter in the configuration parameter set. So the way described above seems to be the workaround.

  1 Comment

Chris
Chris on 19 Oct 2016
Thank you for your answer, Swarooph. I definitely consider it an easy solution :-)
I didn't know about setActiveConfigSet. In my case I have freestanding configuration sets, so I had to load the model and attach the configuration sets first.
load_system( myModelName )
attachConfigSet( myModelName , myConfigSet )
setActiveConfigSet( myModelName, get( myConfigSet, 'Name'))
simOut = sim( [myModelName '.mat'], 'SrcWorkspace','current')
This (basically) works great.
Unfortunately my model contains reference submodels... and those cause problems, as they don't find the correct workspace. Seems, I need to spend some more time on this.

Sign in to comment.

More Answers (0)

Sign in to answer this question.