Is it possible to update the parameters of a block using the output of another block in Simulink?

42 views (last 30 days)
I want to update a parameter found in the mask of a block with the value of the signal that is output by a different block. For example, I would like to set the 'Phase' parameter associated with the Sine Wave block to the signal value output by a Pulse Generator block.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 11 Oct 2012
Simulink provides assorted blocks featuring inports that can be used to set dynamically the values of parameters that are commonly found in the block's mask. For example, the following blocks accept inputs that specify parameter values, in lieu of setting those parameter values in the block's dialog box:
  • Dead Zone Dynamic block
  • Saturation Dynamic block
  • Rate Limiter Dynamic block
  • Transfer Fcn Direct Form II Time Varying block
  • Integrator block (i.e., using an external initial condition)
  • Discrete-Time Integrator block (i.e., using an external initial condition)
Also note that several Unit Delay blocks featuring a variety of such inports are available in the "Simulink -> Additional Math & Discrete -> Additional Discrete" library.
However, there are many blocks that do not provide inports for controlling the values of certain parameters. To work around this issue, implement the desired functionality using basic Simulink blocks.
For instance, to create a Sine Wave block that accepts frequency, phase, amplitude, and bias parameters via inports, implement the following equation using basic Simulink blocks:
y = amplitude * sin(frequency * time + phase) + bias
The attached model titled sinewave_example.mdl demonstrates an example of this.
Note that this type of implementation may hold trade-offs that are important to understand. This subject is discussed further in the "Waveform Generation: Sine Waves and Distortion Measurement" demo that ships with Simulink. To access the demo, type the following at the MATLAB prompt:
demo simulink general
Then click on the "Waveform Generation: Sine Waves and Distortion Measurement" demo.
What about using the To Workspace block or SET_PARAM function to achieve this aim?
Variables in the workspace cannot be continuously updated during simulation using the To Workspace block. The variables are not created in the workspace until the simulation has been paused or completed. Also note that variables specified as parameters in a Simulink block dialog box are evaluated in the MATLAB workspace at the beginning of the simulation. The values of those variables are not updated without explicitly updating the block diagram, e.g., by selecting the "Edit -> Update Diagram" menu item.
Alternatively, the SET_PARAM function can be used in a MATLAB file S-function to modify dynamically the values of parameters in a block's dialog during simulation. However, there are many drawbacks associated with this approach:
  • Obfuscates the connections between blocks. Blocks that share a time-varying dependence are naturally expressed with signal line connections in Simulink's graphical modeling environment. When SET_PARAM is used, then there is no clear visual connection between such blocks.
  • Connecting blocks with signal lines allows Simulink to establish dependencies on the information exchanges between blocks. Using SET_PARAM does not allow Simulink to recognize this dependence that is actually present. This can lead to unexpected results in some cases.
  • Diminishes performance. Using SET_PARAM in a MATLAB file S-function requires a call to the MATLAB Interpreter at every time step during the simulation, which diminishes the performance of Simulink.
  • Prevents code generation. Using SET_PARAM in a MATLAB file S-function is not supported for code generation using Real-Time Workshop.
A final workaround is to use the SET_PARAM function from within an Embedded MATLAB Function block, as shown in the model "EML_example.mdl".

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!