Main Content

coder.getArgTypes

Determine types of function input arguments by executing test file

Description

example

types = coder.getArgTypes(test_fcn,fcn) returns a cell array of coder.Type objects determined by executing test_fcn. The function test_fcn must call the specified entry-point MATLAB® function fcn. Input arguments to fcn construct the returned types.

example

struct_of_types = coder.getArgTypes(test_fcn,{fcn_1,...,fcn_n}) returns a structure containing cell arrays of coder.Type objects determined by executing test_fcn. The function test_fcn must call the specified entry-point functions fcn_1 through fcn_n. Input arguments to these functions construct the returned types. The returned structure contains one field for each function. The field name is the same as the name of the corresponding function.

example

struct_of_types = coder.getArgTypes(test_fcn,fcn, 'uniform',struct_flag) returns a structure even though there is only one entry-point function. The property uniform defines whether the output array type is a structure of cell arrays (true) or a cell array (false).

Examples

collapse all

Get input parameter types for function my_fun by running test file my_test, which calls my_fun. Use these input types to generate code for my_fun.

In a local writable folder, create the MATLAB function my_fun and save it in the file my_fun.m.

function y = my_fun(u,v) %#codegen
    y = u+v;
end

Create the test function my_test and save it in the file my_test.m.

function y = my_test
    a = single(10);
    b = single(20);
    y = my_fun(a,b);
end

To get the input types for my_fun, run the test function.

types = coder.getArgTypes('my_test','my_fun')
types =

  1×2 cell array

    {1×1 coder.PrimitiveType}    {1×1 coder.PrimitiveType}

Generate a MEX function for my_fun by using these input types as example inputs.

codegen my_fun -args types

In the current folder, codegen generates a MEX function, my_fun_mex, that accepts inputs of type single.

Test the MEX function. For example:

 y = my_fun_mex(single(10),single(20))

Get input parameter types for functions my_fun1 and my_fun2 by running test file my_test2, which calls my_fun1 and my_fun2. Use these input types to generate code for my_fun1 and my_fun2.

In a local writable folder, create the MATLAB function my_fun1. Save it in the file my_fun1.m.

function y = my_fun1(u) %#codegen
    y = u;

Create the function my_fun2. Save it in the file my_fun2.m.

function y = my_fun2(u,v) %#codegen
    y = u + v;

Create the test function.

function [y1, y2] =  my_test2
    a = 10;
    b = single(20);
    y1 = my_fun1(a);
    y2 = my_fun2(a,b);
end

To get the input types for my_fun1 and my_fun2, run the test function.

types = coder.getArgTypes('my_test2',{'my_fun1','my_fun2'})
types = 

  struct with fields:

    my_fun1: {[1×1 coder.PrimitiveType]}
    my_fun2: {[1×1 coder.PrimitiveType]  [1×1 coder.PrimitiveType]}

Generate a MEX function for my_fun1 and my_fun2 by using these input types as example inputs.

codegen my_fun1 -args types.my_fun1 my_fun2 -args types.my_fun2

In the current folder, codegen generates a MEX function, my_fun1_mex, with two entry points, my_fun1 and my_fun2, that accept inputs of type double.

Test each entry point in the MEX function. For example:

y1 = my_fun1_mex('my_fun1',10)
y2 = my_fun1_mex('my_fun2',10,20)

Get input parameter types for function my_fun by running test file my_test, which calls my_fun. Use these input types to generate code for my_fun.

In a local writable folder, create the MATLAB function my_fun and save it in the file my_fun.m.

function y = my_fun(u,v) %#codegen
    y = u+v;
end

Create the test function my_test and save it in the file my_test.m.

function y = my_test
    a = single(10);
    b = single(20);
    y = my_fun(a,b);
end

To get the input types for my_fun as structure with fields, run the test function.

types = coder.getArgTypes('my_test','my_fun','uniform',true)
types =

   struct with fields:

    my_fun: {[1×1 coder.PrimitiveType]  [1×1 coder.PrimitiveType]}

Generate a MEX function for my_fun by using these input types as example inputs.

codegen my_fun -args types.my_fun

In the current folder, codegen generates a MEX function, my_fun_mex, that accepts inputs of type single.

Test the MEX function. For example:

 y = my_fun_mex(single(10),single(20))

Input Arguments

collapse all

Name or handle of entry-point MATLAB function for which you want to determine input types. The function cannot be a local function. It must be on the MATLAB path in a writable folder.

Example: types = coder.getArgTypes('my_test','my_fun');

Example: types = coder.getArgTypes(@my_test,@my_fun);

Data Types: char | string | function_handle

Comma-separated list of names or handles of entry-point MATLAB functions for which you want to determine input types. The functions cannot be local functions. They must be on the MATLAB path in a writable folder. The entry-point function names must be unique.

Example: types = coder.getArgTypes('my_test',{'my_fun1','my_fun2'});

Example: types = coder.getArgTypes(@my_test,{@my_fun1,@my_fun2});

Data Types: char | string | function_handle

Name or handle of test function or name of test script. The test function or script must be on the MATLAB path. test_fcn must call at least one of the specified entry-point functions. The input arguments to these functions construct the returned types.

Example: types = coder.getArgTypes('my_test','my_fun');

Data Types: char | string | function_handle

trueReturns a structure of cell arrays of coder.Type objects determined by executing test_fcn, even though there is only one entry-point function.
falseReturns a cell array of coder.Type objects determined by executing test_fcn.

Example: coder.getArgTypes('my_test','my_fun','uniform',true);

Data Types: logical

Output Arguments

collapse all

Cell array of coder.Type objects determined by executing the test function.

Structure containing cell arrays of coder.Type objects determined by executing the test_fcn function. The structure contains one field for each function. The field name is the same as the name of the corresponding function.

Tips

  • Before using coder.getArgTypes, run the test function in MATLAB to verify that it provides the expected results.

  • Verify that the test function calls the specified entry-point functions with input data types suitable for your run-time environment. If the test function does not call a specified function, coder.getArgTypes cannot determine the input types for this function.

  • coder.getArgTypes might not compute the ideal type for your application. For example, you might want the size to be unbounded. The coder.getArgTypes function returns a bound based on the largest input. To adjust the sizes of the returned types, use coder.resize.

  • For some combinations of inputs, coder.getArgTypes cannot produce a valid type. For example, if the test function calls the entry-point function with single inputs, and then calls it with double inputs, coder.getArgTypes generates an error because there is no single type that can represent both calls.

  • When you generate code for the MATLAB function, use the returned types as example inputs by passing them to the codegen function using the -args option.

Introduced in R2012a