coder.ref
Indicate data to pass by reference
Syntax
coder.ref(arg)
Description
coder.ref(
indicates thatarg
)arg
is an expression or variable to pass by reference to an external C/C++ function. Usecoder.ref
inside acoder.ceval
call only. The C/C++ function can read from or write to the variable passed by reference. Use a separatecoder.ref
construct for each argument that you pass by reference to the function.
Examples
Pass Scalar Variable by Reference
Consider the C functionaddone
that returns the value of an input plus one:
double addone(double* p) { return *p + 1; }
The C function defines the input variablep
as a pointer to a double.
Pass the input by reference toaddone
:
...y = 0; u = 42; y = coder.ceval('addone', coder.ref(u));...
Pass Multiple Arguments by Reference
...u = 1; v = 2; y = coder.ceval('my_fcn', coder.ref(u), coder.ref(v));...
Pass Class Property by Reference
...x = myClass; x.prop = 1; coder.ceval('foo', coder.ref(x.prop));...
Pass a Structure by Reference
To indicate that the structure type is defined in a C header file, usecoder.cstructname
.
Suppose that you have the C functionincr_struct
. This function reads from and writes to the input argument.
#include "MyStruct.h" void incr_struct(struct MyStruct *my_struct) { my_struct->f1 = my_struct->f1 + 1; my_struct->f2 = my_struct->f2 + 1; }
The C header file,MyStruct.h
, defines a structure type namedMyStruct
:
#ifndef MYSTRUCT #define MYSTRUCT typedef struct MyStruct { double f1; double f2; } MyStruct; void incr_struct(struct MyStruct *my_struct); #endif
In your MATLAB®function, pass a structure by reference toincr_struct
. To indicate that the structure type fors
has the nameMyStruct
that is defined in the C header fileMyStruct.h
, usecoder.cstructname
.
functiony = foo%#codegeny = 0; coder.updateBuildInfo('addSourceFiles','incr_struct.c'); s = struct('f1',1,'f2',2); coder.cstructname(s,“MyStruct”,'extern','HeaderFile','MyStruct.h'); coder.ceval('incr_struct', coder.ref(s));
To generate standalone library code, enter:
codegen-config:libfoo-report
Pass Structure Field by Reference
...s = struct('s1', struct('a', [0 1])); coder.ceval('foo', coder.ref(s.s1.a));...
You can also pass an element of an array of structures:
...c = repmat(struct('u',magic(2)),1,10); b = repmat(struct('c',c),3,6); a = struct('b',b); coder.ceval('foo', coder.ref(a.b(3,4).c(2).u));...
Input Arguments
Limitations
You cannot pass these data types by reference:
Class or System object
Cell array or index into a cell array
If a property has a get method, a set method, or validators, or is a System object property with certain attributes, then you cannot pass the property by reference to an external function. SeePassing By Reference Not Supported for Some Properties.
Tips
If
arg
is an array, thencoder.ref(arg)
provides the address of the first element of the array. Thecoder.ref(arg)
function does not contain information about the size of the array. If the C function must know the number of elements of your data, pass that information as a separate argument. For example:coder.ceval('myFun',coder.ref(arg),int32(numel(arg));
When you pass a structure by reference to an external C/C++ function, use
coder.cstructname
to provide the name of a C structure type that is defined in a C header file.In MATLAB,
coder.ref
results in an error. To parameterize your MATLAB code so that it can run in MATLAB and in generated code, usecoder.target
.You can use
coder.opaque
to declare variables that you pass to and from an external C/C++ function.