Prepare component for simulation


function setup


function setup

The body of the setup function can contain assignment statements, if and error statements, and across and through functions. The setup function is executed once for each component instance during model compilation. It takes no arguments and returns no arguments.

Use the setup function for the following purposes:

  • Validating parameters

  • Computing derived parameters

  • Setting initial conditions

The following rules apply:

  • The setup function is executed as regular MATLAB® code.

  • All members declared in the component are available by their name.

  • All members (such as variables, parameters) that are externally writable are writable within setup. See Member Summary for more information.

  • Local MATLAB variables may be introduced in the setup function. They are scoped only to the setup function.

The following restrictions apply:

  • Command syntax is not supported in the setup function. You must use the function syntax. For more information, see Command vs. Function Syntax in the MATLAB Programming Fundamentals documentation.

  • Persistent and global variables are not supported. For more information, see Persistent Variables and Global Variables in the MATLAB Programming Fundamentals documentation.

  • MATLAB system commands using the ! operator are not supported.

  • try-end and try-catch-end constructs are not supported.

  • Nested functions are not supported.

  • Passing declaration members to external MATLAB functions, for example, my_function(param1), is not supported. You can, however, pass member values to external functions, for example, my_function(param1.value).


The following setup function checks the value of a parameter MyParam, declared in the declaration section of a component file. It defines a maximum allowed value for this parameter, MaxValue, and if MyParam is greater than MaxValue, overrides it with MaxValue and issues a warning.

function setup
   MaxValue = {1, 'm' };
   if MyParam > MaxValue 
      warning( 'MyParam is greater than MaxValue, overriding with MaxValue' );
      MyParam = MaxValue;
Was this topic helpful?