| Using Simulink | ![]() |
Declaring Local Variables Implicitly
You declare variables implicitly in Embedded MATLAB functions by specifying constants and making initial assignments. By default, this type of variable does not persist between function calls. It is recreated for each function call and has no identity or value outside the Embedded MATLAB function. You can, however, make this variable persistent with a persistent statement at the top of the function body before the first use of the variable. For example, to declare the variable abc persistent, enter the following line right after the function header and comments:
You declare variables implicitly in the function body of Embedded MATLAB functions in the following ways:
The first use of a variable must initialize its value, its type, and its size. For example, the following initial assignments declare variables in an Embedded MATLAB function:
The following rules apply to the use of variables that you declare implicitly in the body of an Embedded MATLAB function:
persistent statement.
In MATLAB, you can declare the size of a variable using indexing. In Embedded MATLAB functions, this is not allowed. For example, the following initial assignment is not allowed in Embedded MATLAB functions:
In MATLAB, the preceding example declares g to be a 3-by-2 array of type double elements, in which the element g(3,2) is 14.6 and all other elements are 0. In Embedded MATLAB functions, this statement is not allowed.
In the following example code, you declare y and z to be integers with the following initial assignments:
| Local Variables in Embedded MATLAB Functions | Declaring Local Complex Variables Implicitly | ![]() |
© 1994-2005 The MathWorks, Inc.