MATLAB: How to pass a structure as a Simulink.Parameter object in Simulink 8.0 (R2012b)

simulink

I would like to pass a Simulink.Parameter object as a structure to a Simulink block using SET_PARAM.
For example:
example.data.Gain(2).p = Simulink.Parameter;
example.data.Gain(2).p.Value = 1;
example.data.Gain(2).p.Min = 0;
example.data.Gain(2).p.Max = 10;
mws = get_param(gcs, 'modelworkspace');
mws.assignin('ex', example );
set_param([sys '/Gain2'], 'Gain', 'ex.data.Gain(2).p');
%%%END CODE
when I try that I get this error:
Expression 'ex.data.Gain(2).p' for block 'test2/Gain2' returns a Simulink.Parameter object that has property values that need to be evaluated. This object should
be instantiated in a workspace and referenced by name in Simulink.

Best Answer

Currently it is not possible to pass a struct to the function SET_PARAM which has not been already instantiated in the workspace.
However, there is something called a 'Structure Parameter" that behaves very similarly to structures but with the additional settable attributes that Parameters have. This is very useful for generating code where these structures should remain tunable.
For example, to use the following syntax to set a Constant block:
 
>> set_param(gcb,'Value','Bottom.PressureChB')
Create a parameter struct in the workspace like this:
 
% Create structure
>> Bottom = Simulink.Parameter;
>> Bottom.Value = struct('PressureChA',1,'PressureChB',2);
>> Bottom.CoderInfo.StorageClass = 'ExportedGlobal';
Alternatively, you can try this method:
  As a workaround you can use a Simulink.Parameter object which contains the structure of the parameters.
To do that you need to:
1. Create the buses such that its elements match the desired parameter structure. (You can have sub-buses as bus elements to create deeper structures)
2. Create a MATLAB structure that corresponds to the Bus using the function Simulink.Bus.createMATLABStruct.
3. Create a Simulink.Parameter object whose datatype is the previously created bus 'Bus: BUSNAME'.
4. Assign the MATLAB structure created above to the parameter object's .Value field.
5. Use this object as a structure in the Simulink model.
See the attachment for an example.