coder.getArgTypes - Determine types of function input arguments by executing test file - MATLAB (original) (raw)
Determine types of function input arguments by executing test file
Syntax
Description
[types](#mw%5Fb38477a4-e7dd-4687-8d57-bc787a63267a) = coder.getArgTypes([test_fcn](#mw%5F1dc8baff-9725-4e3e-8d10-be84b36ba0d1),[fcn](#mw%5F0d3461a8-04c4-4a71-96d9-73da538a3e38))
returns a cell array of coder.Type
objects determined by executing test_fcn
. The functiontest_fcn
must call the specified entry-point MATLAB® function fcn
. Input arguments tofcn
construct the returned types.
[struct_of_types](#mw%5F0a88a204-d983-43c5-ba19-0c62c3c79456) = coder.getArgTypes([test_fcn](#mw%5F1dc8baff-9725-4e3e-8d10-be84b36ba0d1),[fcns](#mw%5F8da74f77-dee9-461b-aeae-12883d94776b))
returns a structure containing cell arrays of coder.Type
objects determined by executingtest_fcn
. The function test_fcn
must call the specified entry-point functions listed in fcns
. 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.
[struct_of_types](#mw%5F0a88a204-d983-43c5-ba19-0c62c3c79456) = coder.getArgTypes([test_fcn](#mw%5F1dc8baff-9725-4e3e-8d10-be84b36ba0d1),[fcn](#mw%5F0d3461a8-04c4-4a71-96d9-73da538a3e38),'uniform',[struct_flag](#mw%5F361be455-54e8-4ad8-ade0-354f6e2298d3))
returns a structure even though there is only one entry-point function. The propertyuniform
defines whether the output array type is a structure of cell arrays (true) or a cell array (false).
Examples
Get input parameter types for function my_fun
by running test file my_test
, which callsmy_fun
. Use these input types to generate code formy_fun
.
In a local writable folder, create the MATLAB function my_fun
and save it in the filemy_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 typesingle
.
Test the MEX function. For example:
y = my_fun_mex(single(10),single(20))
Get input parameter types for functions my_fun1
andmy_fun2
by running test file my_test2
, which calls my_fun1
and my_fun2
. Use these input types to generate code for my_fun1
andmy_fun2
.
In a local writable folder, create the MATLAB function my_fun1
. Save it in the filemy_fun1.m
.
function y = my_fun1(u) %#codegen y = u;
Create the function my_fun2
. Save it in the filemy_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
andmy_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
andmy_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 callsmy_fun
. Use these input types to generate code formy_fun
.
In a local writable folder, create the MATLAB function my_fun
and save it in the filemy_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 typesingle
.
Test the MEX function. For example:
y = my_fun_mex(single(10),single(20))
Input Arguments
Name or handle of entry-point MATLAB function for which you want to determine input types. The function cannot be a local function or a function in a namespace. 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
Names or handles of entry-point MATLAB functions for which you want to determine input types. The functions cannot be local functions or functions in namespaces. 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});
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
true | Returns a structure of cell arrays of coder.Type objects determined by executing test_fcn, even though there is only one entry-point function. |
---|---|
false | Returns a cell array of coder.Type objects determined by executingtest_fcn. |
Example: coder.getArgTypes('my_test','my_fun','uniform',true);
Data Types: logical
Output Arguments
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
coder.getArgTypes
returns the input types of the function arguments, similar to the Automatically Define Input Types step in the app.- 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. Thecoder.getArgTypes
function returns a bound based on the largest input. To adjust the sizes of the returned types, usecoder.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.
Alternatives
- Define Types of Entry-Point Inputs by Using the MATLAB Coder App
- Specify Input Types Using assert Statements in MATLAB Code
Version History
Introduced in R2012a