Run sim command with configset from within a ML-function

10 次查看(过去 30 天)
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)

采纳的回答

Swarooph
Swarooph 2016-10-18
编辑:Swarooph 2016-10-18
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.
  2 个评论
Chris
Chris 2016-10-19
编辑:Chris 2016-10-19
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.
Noam Greenboim
Noam Greenboim 2024-4-17
It didn't work for me with the code of @Swarooph , so eventually I used the following code to push my configuration parameters into the model and run the simulation:
[~,mdl_name] = fileparts(sys); % 'sys' is a string of the simulink file path
load_system(sys) % have to load it, in order to make the changes
configSet = getActiveConfigSet(mdl_name); % get the current configuration, in order to modify it
fn = fieldnames(config); % 'config' is a struct with name-value pairs of configuration parameters
for i=1:length(fn) % modify the relevant configSet object, with my parameters
configSet.set_param(fn{i},config.(fn{i}));
end
simOut = sim(sys, 'SrcWorkspace','parent'); % run the simulation in the caller's workspace

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by