Main Content

Define Input Parameter by Example by Using the App

Define an Input Parameter by Example

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

    App menu, showing Define by Example menu item

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter a MATLAB® expression. The variable has the class, size, and complexity of the value of the expression.

    Alternatively, you can select a variable from the list of workspace variables that displays.

    App window, showing list of variables

Specify Input Parameters by Example

This example shows how to specify a 1-by-4 vector of unsigned 16-bit integers.

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter:

    zeros(1,4,'uint16')

    The input type is uint16(1x4).

  5. Optionally, after you specify the input type, you can specify that the input is variable size. For example, select the second dimension.

    App dialog box, showing variable-sizing choices for the second dimension of variable A

  6. To specify that the second dimension is variable size with an upper bound of 4, select :4. Alternatively, to specify that the second dimension is unbounded, select :Inf.

Alternatively, you can specify that the input is variable size by using the coder.newtype function. Enter the MATLAB expression:

coder.newtype('uint16',[1 4],[0 1])

Note

To specify that an input is a double-precision scalar, enter 0.

Specify a String Scalar Input Parameter by Example

This example shows how to specify a string scalar type by providing an example string.

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter:

    "mystring"

    The input parameter is a 1-by-1 string array (string scalar) that contains a 1-by-8 character vector.

    App window, showing contents of string scalar s

  5. To make the string variable-size, click the second dimension.

    • To specify that the second dimension is unbounded, select :Inf.

    • To specify that the second dimension has an upper bound, enter the upper bound, for example 8. Then, select :8.

Specify a Structure Type Input Parameter by Example

This example shows how to specify a structure with two fields, a and b. The input type of a is scalar double. The input type of b is scalar char.

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter:

    struct('a', 1, 'b', 'x')
    

    The type of the input parameter is struct(1x1). The type of field a is double(1x1). The type of field b is char(1x1)

  5. For an array of structures, to specify the size of each dimension, click the dimension and specify the size. For example, enter 4 for the first dimension.

  6. To specify that the second dimension is variable size with an upper bound of 4, select :4. Alternatively, to specify that the second dimension is unbounded select :Inf.

Alternatively, specify the size of the array of structures in the struct function call. For example, struct('a', { 1 2}, 'b', {'x', 'y'}) specifies a 1x2 array of structures with fields a and b. The type of field a is double(1x1). The type of field b is char(1x1).

To modify the type definition, see Specify a Structure Input Parameter.

Specify a Cell Array Type Input Parameter by Example

This example shows how to specify a cell array input by example. When you define a cell array by example, the app determines whether the cell array is homogeneous or heterogeneous. See Code Generation for Cell Arrays. If you want to control whether the cell array is homogeneous or heterogeneous, specify the cell array by type. See Specify a Cell Array Input Parameter.

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter an example cell array.

    • If all cell array elements have the same properties, the cell array is homogeneous. For example, enter:

      {1 2 3}
      
      The input is a 1x3 cell array. The type of each element is double(1x1).

      App window, showing the types and sizes of elements in a homogenous cell array

      The colon inside curly braces{:} indicates that all elements have the same properties.

    • If elements of the cell array have different classes, the cell array is heterogeneous. For example, enter:

      {'a', 1}
      The input is a 1x2 cell array. For a heterogeneous cell array, the app lists each element. The type of the first element is char(1x1). The type of the second element is double(1x1).

      App window, showing the types and sizes of elements in a heterogeneous cell array

    • For some example cell arrays. the classification as homogeneous or heterogeneous is ambiguous. For these cell arrays, the app uses heuristics to determine whether the cell array is homogeneous or heterogeneous. For example, for the example cell array, enter:

      {1 [2 3]}
      The elements have the same class, but different sizes. The app determines that the input is a 1x2 heterogeneous cell array. The type of the first element is double(1x1). The type of the second element is double(1x2).

      App window, showing the types and sizes of elements in an ambiguously heterogeneous cell array

      However, the example cell array, {1 [2 3]}, can also be a homogeneous cell array whose elements are 1x:2 double. If you want this cell array to be homogeneous, do one of the following:

      • Specify the cell array input by type. Specify that the input is a homogeneous cell array. Specify that the elements are 1x:2 double. See Specify a Cell Array Input Parameter.

      • Right-click the variable. Select Homogeneous. Specify that the elements are 1x:2 double.

      If you use coder.typeof to specify that the example cell array is variable size, the app makes the cell array homogeneous. For example, for the example input, enter:

      coder.typeof({1 [2 3]}, [1 3], [0 1])
      The app determines that the input is a 1x:3 homogeneous cell array whose elements are 1x:2 double.

To modify the type definition, see Specify a Cell Array Input Parameter.

Specify an Enumerated Type Input Parameter by Example

This example shows how to specify that an input uses the enumerated type MyColors.

Suppose that MyColors.m is on the MATLAB path.

classdef MyColors < int32
    enumeration
        green(1),
        red(2),
    end
end

To specify that an input has the enumerated type MyColors:

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

    App menu, showing Define by Example menu item

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter the MATLAB expression:

    MyColors.red

Specify an Object Input Type Parameter by Example

This example shows how to specify the type for an object of a value class myRectangle.

classdef myRectangle
    properties
        length;
        width;
    end
    methods
        function obj = myRectangle(l,w)
            if nargin > 0
                obj.length = l;
                obj.width = w;
            end
        end
        function area = calcarea(obj)
            area = obj.length * obj.width;
        end
    end
end

  1. Define a function that takes an object of the value class as an input. For example:

    function z = getarea(r)
    %#codegen
    z = calcarea(r);
    end

  2. In MATLAB, define an object rect_obj.

    rect_obj = myRectangle(3,4)

  3. In the app, on the Select Source Files page, enter getarea for the entry-point function.

  4. On the Define Input Types page, click Let me enter input or global types directly.

  5. Click the field to the right of r.

  6. Select Define by Example.

  7. In the field to the right of r, enter rect_obj or select it from the list of workspace variables. The app determines that r is a class with properties length and width.

Alternatively, you can provide a coder.ClassType object for that class. To define a coder.ClassType object, use coder.typeof. For example:

  1. In MATLAB, define a coder.ClassType object that has the same properties as rect_obj.

    t = coder.typeof(rect_obj)

  2. In the app, provide t as the example.

To change the size or type of a property, click the field to the right of the property.

When you generate code, the properties that you define in the app must be consistent with the properties in the class definition file. If the class definition file has properties that your code does not use, your type definition in the app does not have to include those properties. The code generator removes properties that your code does not use.

See Specify Objects as Inputs in the MATLAB Coder App.

Specify a Fixed-Point Input Parameter by Example

To specify fixed-point inputs, Fixed-Point Designer™ software must be installed.

This example shows how to specify a signed fixed-point type with a word length of eight bits, and a fraction length of three bits.

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define.

  3. Select Define by Example.

  4. In the field to the right of the parameter, enter:

    fi(10, 1, 8, 3) 

    The app sets the type of input u to fi(1x1). By default, if you do not specify a local fimath, the app uses the default fimath. See fimath for Sharing Arithmetic Rules (Fixed-Point Designer).

    Optionally, modify the fixed-point properties or the size of the input. See Specify a Fixed-Point Input Parameter and Specify Types of Entry-Point Inputs Using the App.

Specify an Input from an Entry-Point Function Output Type

When generating code for multiple entry-point functions, you can use the output type from one entry-point function as the input type to another entry-point function. For more information, see Pass an Entry-Point Function Output as an Input.

  1. On the Define Input Types page, click Let me enter input or global types directly.

  2. Click the field to the right of the input parameter that you want to define and select Use Output.

    App menu, showing Use Output menu item

  3. Select the name of the entry-point function and the corresponding output parameter from which to define the input type.

The MATLAB Coder™ app is not supported in MATLAB Online™.