How do I run a model in Simulink from my MATLAB function where my Simulink parameters are defined in this function?

323 views (last 30 days)
I want to simulate a model using the "sim" command from inside a MATLAB function. My model has mask parameters that are variables defined in my function, "fcn_name.m". I know that Simulink uses the variables stored in the base workspace.
Is there a way to simulate my model from inside my function without creating variables in the base workspace?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Feb 2023
Edited: MathWorks Support Team on 16 Apr 2023
There are several ways to make Simulink use variables that you specify inside a function without placing them in the base workspace. A few of them are listed below.
1. Use a "Simulink.SimulationInput" object
The command "sim" accepts objects of the type "Simulink.SimulationInput". You can use a SimulationInput object to temporarily change your model and specify parameters, variables, initial state, etc.
To set a variable, use function "setVariable". For example, to simulate model "f14" with variable "Beta" as 100:
modelname = "f14";
simIn = Simulink.SimulationInput(modelname);
simIn = setVariable(simIn,'Beta',100);
sim(simIn);
If you have a large number of variables to change in your model, you might particularly like the "loadVariablesFromMATFile" function, which as the name implies, loads variables from a MAT file. Alternatively, you can loop the call to 'setVariable' like it is done with 'assignin' in the next option.
The object "Simulink.SimulationInput" is documented here: https://www.mathworks.com/help/simulink/slref/simulink.simulationinput-class.html
The function "setVariable" is documented here: https://www.mathworks.com/help/simulink/slref/simulink.simulationinput.setvariable.html
The function "loadVariablesFromMATFile" is documented here: https://www.mathworks.com/help/simulink/slref/simulink.simulationinput.loadvariablesfrommatfile.html
2. Change the Model Workspace
Each Simulink model is provided with its own workspace for storing variable values, and that takes precedence over the base workspace. You can change the model workspace from your function, or change the MAT-file that is the data source for the model workspace.
This code shows how to programmatically add the functions' variables to the model workspace:
hws = get_param(modelname, 'modelworkspace');
% Here it is possible to define as many parameters as possible
a = 5;
list = whos; % Get the list of variables defined in the function
N = length(list);
% Assign everything in the model workspace
for i = 1:N
hws.assignin(list(i).name,eval(list(i).name));
end
You can find more information here: https://www.mathworks.com/help/simulink/ug/using-model-workspaces.html
3. Work with Simulink Data Dictionaries
Your model can use variables from a Simulink Data Dictionary. As with the Model Workspace, you can programmatically change the Data Dictionary used by the Simulink model, or even create a new Data Dictionary to replace an old one.
More information about using Data Dictionaries programmatically is located here: https://www.mathworks.com/help/simulink/ug/store-data-in-dictionary-programmatically.html
4. Use 'SrcWorkspace'
This option is not recommended after release R2009b.
The Name-Value option 'SrcWorkspace' can change the workspace used by the model. Use
simOut = sim( modelname, 'SrcWorkspace', 'current')
or set it as an option with 'simset':
options = simset('SrcWorkspace','current');
sim('modelname',[],options)
For information on how to create variables in the base workspace from inside a function, visit the URL:
  1 Comment
Delprat Sebastien
Delprat Sebastien on 7 May 2022
This trend of making things always more complex and unefficient for simple things is really annoying. So basically, because the software offers more structured approach, you loose the capability of doing simpled but efficient things....The trend is do the full stuff or be limited to very basic features... In a next step, will they dare to make simulink project mandatory ?
In the previous versions, you could run a model from a function or the base workspace using a simple 2 line command using options = simset('SrcWorkspace','current'); sim('modelname',[],options) . This was enough for many tasks. Efficient, quick and reliable programming.
So what I understand is that now, they force us to use model workspace or the base workspace. Meaning that you do not have any simple approach to work from within a function.
I can understand that for some big project, with may fixed parameters, model workspace is usefull.... come on, simply wiping the capability of using a function workspace is just frustrating.
I have a model with many parameters and I want to perform simulations with many combinations of parameters values, and this is done from within a function. So I need to :
1) make a list of all the variables I need to send to the workspace. This makes the program evolution quite difficult : if I add a block with some new parameters in Simulink, not only I have to define the value from within my function (normal) but also I have to add it to the list of variable to be transfered...
2) make a loop for all the variable in the list and dynamically assign them to the workspace using assignin(mdlWks,varList(i),eval(varList(i)));
Alternatively, I may assign value in the baseworkspace using assigning with is an even worse idea..
=> Both are highly unefficient way of programming...
grrrrrr

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products


Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!