This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Prepare component for simulation


function setup

function setup %#simple


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.

  • In case of conflict, assignments in the setup section override those made in the declaration section.

  • 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 (MATLAB) in the MATLAB Programming Fundamentals documentation.

  • Persistent and global variables are not supported. For more information, see Persistent Variables (MATLAB) and Global Variables (MATLAB) 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).

In general, you cannot designate a block parameter as run-time if the underlying component uses it in the setup function. However, if the setup is restricted to simple operations like error-checking, you can declare the setup function as simple:

function setup %#simple

In this case, many of the parameters used in the setup function can be designated as run-time parameters. For more information, see Simple Setup.


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;

Introduced in R2008b

Was this topic helpful?