MATLAB: How to assign a variable from the base workspace to a Simulink.Parameter object in the model workspace in Simulink 7.2 (R2008b)

assiginmodelsimulinkworkspace

I have a system with number of blocks. I want to assign a variable X1 from the base workspace to a Simulink.Parameter object X in the model workspace. Further I want to change the variable X1, save, close the model and expect the parameter X to obtain this new value of X1 when the model is reloaded.

Best Answer

In order to do this using Simulink 7.2 (R2008b), you will need to use a combination of PRELOADFCN and POSTLOADFCN. The following steps illustrate how you can achieve this:
1) Load the variable X1 in the PRELOADFCN of the model.
2) Assign the value of X1 to that of X in the Model Explorer window. Please note if X1 was also a Simulink.Parameter object, one will need to use the value field to assign the value. For instance in the value field, one needs to type X1.value instead of just assigning X1.
3) In the POSTLOADFCN, add the following lines of code:
hws = get_param(bdroot, 'modelworkspace');
hws.assignin('X', X1);
This will ensure that if X1 is changed and the model is saved and closed, the changes will be reflected in X when the model is reloaded.