0
votes

I have a simple transfer function model with with a single variable input (Gain, referred to as 'k'). I am trying to run 10 simulations with the value of 'k' starting at 1 and increasing by a value of 1 with each iteration. The first way I tried to do this was following a tutorial with the following code:

for k = 1:10;
    data(k) = sim ('model_name', 'ReturnWorkspaceOutputs', 'on');   
end

This ended up giving me 1x10 simulation output matrix with the same output data (as if k had a value of 5 each time). If I run the sim once with k = 1 the output is still as if k had a value of 5. The model-sim works as expected when launched from simulink (not launched from matlab). I then tried creating 10 input class objects using the following code as per the documentation on Simulink input class with the following code.

for l = 1:10;
    in = Simulink.SimulationInput('model_name');
    in = in.setBlockParameter('k', 'Value', 'l');
    data(l) = sim ('in', 'ReturnWorkspaceOutputs', 'on');   
end

The error I am getting here is "Undefined variable "Simulink" or class "Simulink.SimulationInput"". My question is this, what is the best (simple) way to run simulations iteratively while changing block parameters with each consecutive iteration? Thank you for your time.

1

1 Answers

1
votes

Your first attempt should work, but if you are running this code in a function, your model will be evaluating the "k" expression for your block parameter in the base workspace (most likely). If "k" was 5 there, it will be 5 for each iteration.

Simulink by default evaluates parameter values in the mask workspace > model workspace > base workspace (and/or in a data dictionary), even if you run the "sim" function in another function's workspace.

However you can run the "sim" command specifying it should use the workspace of the function from which you call "sim" like this:

for k = 1:10;
    data(k) = sim('model_name', 'ReturnWorkspaceOutputs', 'on', 'SrcWorkspace', 'current'); 
end