askvity

How to Declare Variable Size in MATLAB (for Code Generation)

Published in MATLAB Code Generation 5 mins read

In standard MATLAB scripting, variables are dynamically sized and do not require explicit size declaration. However, when working with code generation or specific environments like MATLAB Function blocks within Simulink, you often need to declare variables as variable-size (or 'varsize') to enable the code generator to handle them correctly.

Here are the primary methods for declaring variable size in these contexts:

Methods for Declaring Variable Size

There are two main approaches, depending on whether you are writing standard MATLAB code intended for code generation or working directly within a MATLAB Function block.

1. Using the coder.varsize Function

The coder.varsize function is a directive used within MATLAB code specifically for the code generator. It informs the code generator that a particular variable's size can change during execution.

  • Purpose: To declare a variable as variable-size for code generation.

  • Syntax:

    coder.varsize('variable_name', [dimensions], [variability])
  • Parameters:

    • variable_name: A string containing the name of the variable you want to declare as variable-size.
    • [dimensions]: A row vector specifying the upper bounds for each dimension of the variable. inf or [] can be used for dimensions that are truly unlimited or whose upper bound is not known, but providing finite bounds is generally recommended for efficiency.
    • [variability] (Optional): A logical vector indicating which dimensions can vary in size. true means the dimension is variable-size, false means it is fixed-size. If omitted, all dimensions with finite bounds greater than 1 are considered variable-size, while dimensions with bound 1 are fixed-size.
  • Example:

    function y = process_data(x)
        % Declare a local variable 'temp_data' as variable-size
        % It's a 2D array, first dimension can vary up to 100, second is fixed at 50
        coder.varsize('temp_data', [100, 50], [true, false]);
    
        % Initialize or assign to temp_data
        temp_data = zeros(size(x,1), 50); % Initial size is based on input x
    
        % ... operations that might change the first dimension of temp_data ...
    
        y = temp_data;
    end

    In this example, temp_data is declared as variable-size. The first dimension can change, but its maximum size is 100. The second dimension is fixed at 50.

2. Using the Symbols Pane and Property Inspector in MATLAB Function Blocks

Within a MATLAB Function block in Simulink, you declare the properties of input, output, and local variables directly using the block's interface. This method is particularly important for defining the characteristics of the block's interface, including variable size.

  • Context: Specifically for variables within a MATLAB Function block.

  • Procedure:

    1. Open the MATLAB Function block.
    2. Access the Symbols pane (usually on the left side). This pane lists all variables (inputs, outputs, parameters, locals) used in the block.
    3. Select the variable you want to declare as variable-size from the Symbols pane.
    4. Open the Property Inspector (usually accessible from the View menu if not already visible).
    5. In the Property Inspector, find the Size property for the selected variable.
    6. Configure the Size property:
      • For variable-size dimensions, specify an upper bound (e.g., [100, 50] means the first dimension is variable with an upper bound of 100, and the second is fixed at 50).
      • You can also explicitly mark dimensions as variable-size or fixed-size.
  • Important Note from Reference:

    To declare variable-size output variables in MATLAB Function blocks, use the Symbols pane and the Property Inspector. If you provide upper bounds in a coder. varsize declaration, the upper bounds must match the upper bounds in the Property Inspector.

    This highlights that for output variables in particular within a MATLAB Function block, the Symbols pane and Property Inspector are the standard way to declare variable size. If you also use coder.varsize for a variable within the code of that block (e.g., a local variable or even an output variable, although less common if declared via the UI), the upper bounds specified in coder.varsize must be consistent with the bounds set in the Property Inspector for that variable.

Summary

Method Context How to Specify Size / Bounds Primary Use
coder.varsize function MATLAB code (for code gen) Function syntax: coder.varsize('var', [bounds], [variability]) Local variables in code generation functions
Symbols Pane & Property Inspector MATLAB Function Block (Simulink) UI Configuration: Select variable, edit 'Size' property in Property Inspector Input, Output, Parameter, Local variables in MF blocks

Using these methods allows the MATLAB code generator to allocate memory efficiently and correctly handle arrays whose size is not fixed at compile time.

Related Articles