How do I set the initial state of a Simulink block programatically?

I am trying to set the initial state for selected blocks in my Simulink model (not for the whole model). According to MATLAB manual, you can use getBlockSimState & setBlockSimState to access and modify SimState of individual blocks.
However, this is not very well documented and I always get an error when trying to use those functions. Is there an example of the correct syntax or is there another recommended way of setting the state?

 Accepted Answer

The easiest way to set the initial state of a block from MATLAB code is to use the "set_param" command. This is essentially the programmatic way to populate a block's dialog. The command uses the following format:
set_param( 'model_name/system_name/block_name', 'parameter_name', 'new_parameter_value' )
The "parameter_name" will vary from block-type to block-type, but you can find out the name of a give block's state (It is usually close to the text in the dialog) in the help under "Block-Specific Parameters". The "new_parameter_value" is usually a string that represents the text that you would have typed in the dialog fields.

5 Comments

Hi Mark, thanks for the useful hint. It works indeed. I needed, to use a little trick, however, to solve my problem. I tried to set the init.state for the 'transfer function' block, which appeared impossible. It worked only after transforming the model into the state-space representation.
sometimes isn't easy to set_param of certain blocks, here's one example for a transfer function
set_param('T2/TF1','Numerator',num2str(Km),'Denominator',[ '[' num2str([Tm 1]) ']']);
I agree. And there are more problems. In my model I want to run a new simulation with init. conditions equal to the final state of the previous run. I use: set_param('mymodel','LoadInitialState','on', 'InitialState', ['xFinal0'])
Here 'xFinal0' is the name of the previous state. Now, if I want to change the init.cond. for just one block, say 'A', using 'set_param('mymodel/A','X0','value')' it appears that the 'LoadInitialState'-option has the higher priority and the latter setting has no effect.
Any suggestion on how to solve it, so I can set the init.cond. for 'A' independently of the rest?
The issue might be addressable by changing your configuration parameters. Specifically, older releases of Simulink offered a lot of places to enter initial conditions, so much so that there was a potential for users to enter contradictory/competing initial conditions (e.g. initial values for an outport and a unit delay immediately upstream from it). I would recommend that you go to the Configuration Parameters->Diagnostics->Data Validity section, and make the following changes:
* Change "Underspecified initialization detection" from "Classic" to "Simplified"
* Enable all three of the diagnostics below it.
This should hopefully provide you with more detailed feedback for any initialization issues that you encounter.
With regards to Paulo's comments about difficulty with more complex "set_param" commands, I would recommend that he use MATLAB workspace variables instead of putting long text in the dialog box. Specifically, you can make MATLAB variables in the workspace and use them in the dialog. For example:
>>Km = 42; % Random example number
>>Tm = 2112; % Random example number
>>den = [Tm, 1];
and then the "set_param" commands become:
set_param('T2/TF1','Numerator', 'Km');
set_param('T2/TF1','Denominator',den);
(By the way, we still support and default to the old style of initialization for compatibility, but encourage people to do it the new way.)
Very useful tip, Mark, thanks.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!