MATLAB Answers

Trying to set parameter values in a Simulink model using a parameter structure

2 views (last 30 days)
Bill Tubbs
Bill Tubbs on 26 Aug 2020
Edited: Bill Tubbs on 29 Aug 2020
I'm following the docmentation here in an attempt to set up a Simulink model so I can apply a set of parameter values to it before running it from a MATLAB script.
The kind of things I want to set are the parameter values of various blocks such as these:
sim_model = "hw6_p3_RSTsim"; % See file hw6_p3_RSTsim.slx
open(sim_model)
set_param(sim_model + '/Delay', 'DelayLength', string(d));
and
path = sim_model + "/S(z)";
numstr = '['+strjoin(string(S))+']';
set_param(path, 'Numerator', numstr);
set_param(path, 'Denominator', '1');
These commands work fine I just have a lot of them to set so this method seems inefficient. Setting them all in one go from a Parameter Structure sounds like a better way to do this.
The first step says I need to 'Collect all numeric variables used by the model into a MATLAB structure' as follows:
Simulink.BlockDiagram.createVarStruct(sim_model, 'ModelParam')
But this raises the following error:
Error using Simulink.slobject.BlockDiagram.createVarStruct (line 50)
Assertion failed.
Error in Simulink.BlockDiagram/createVarStruct
I have no idea what this means but the code at line 50 is part of a try except block which has the comment: Check that there isn't already a variable with that name.
The following command also doesn't return any variables in the 'base workspace' so this suggests I might be doing something wrong or there is something wrong with my model.
varList = Simulink.findVars(sim_model, 'WorkspaceType', 'base')
Returns:
varList =
0×0 VariableUsage array with properties:
Name
Source
SourceType
Users
If I go into the Model Editor in Simulink I can see the parameters I wish to set:

Answers (1)

Bill Tubbs
Bill Tubbs on 29 Aug 2020
Edited: Bill Tubbs on 29 Aug 2020
I gave up on trying to figure out the ParameterStructure method and ended up doing it this way which I assume is the standard way to assign values to variables in a Simulink model workspace. After creating variables in Simulink for all the parameters I want to be able to adjust, I then set their values as follows:
% Set model parameters
mws = get_param(sim_model, 'modelworkspace');
mws.assignin('A', BA.Denominator{1})
mws.assignin('B', BA.Numerator{1})
mws.assignin('C', CD.Numerator{1})
mws.assignin('D', CD.Denominator{1})
mws.assignin('d', d) % Process delay
mws.assignin('Vw', Vw) % Noise variance
mws.assignin('seed', 1) % Random number generator seed
mws.assignin('R', R) % Filters are in z^-1
mws.assignin('S', S) % Filters are in z^-1
mws.assignin('T', T) % Filters are in z^-1
mws.assignin('r0', 1) % Reference step magnitude
If anyone can explain how to set up the ParameterStructure mentioned in the question or has a more convenient way to specify a batch of model parameters please let me know!
Ideally what I'd like to do is have something like this as the starting point for my parameter settings and 'pass' it to Simulink before the starting the simulation:
params = struct( ...
'A', BA.Denominator{1}, ...
'B', BA.Numerator{1}, ...
'C', CD.Numerator{1}, ...
'D', CD.Denominator{1}, ...
'd', d, ... Process delay
'Vw', Vw, ... Noise variance
'seed', 1, ... Random number generator seed
'R', R, ... Filters are in z^-1
'S', S, ... Filters are in z^-1
'T', T, ... Filters are in z^-1
'r0', 1 ...
)
I find that structs are the closest thing to a Python dictionary and good for storing and managing sets of parameters with names.
Obviously, I could just create a for loop and assignin each item in the struct one after the other, but I thought there might be a better way to do this.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!