Run sim command with configset from within a ML-function
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Chris
 el 18 de Oct. de 2016
  
    
    
    
    
    Comentada: Noam Greenboim
      
 el 17 de Abr. de 2024
            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 comentarios
Respuesta aceptada
  Swarooph
    
 el 18 de Oct. de 2016
        
      Editada: Swarooph
    
 el 18 de Oct. de 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.
2 comentarios
  Noam Greenboim
      
 el 17 de Abr. de 2024
				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
Más respuestas (0)
Ver también
Categorías
				Más información sobre Programmatic Model Editing en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


