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

Illustration
Benjamin - 2020-09-21T09:41:13+00:00
Question: 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?

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

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.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!