I have a struct with very large arrays in it. I use MATLAB coder to generate C code.
In my generated code I wish to call some C function and pass by reference one of the arrays.
For example:
coder.ceval('Foo',coder.ref(MyStruct.VeryLargeArray));
This is not allowed by MATLAB coder and I get the error:
coder.ref may only be applied to an expression of the type V or V(E) ...
Since MyStruct.VeryLargeArray
is very large, as the name suggests, I wish to avoid the abvious solution of copying it to a temporary varliable:
UnnecessaryTempVar = MyStruct.VeryLargeArray;
coder.ceval('Foo',coder.ref(UnnecessaryTempVar));
Any ideas for a workaround?
You could write a C wrapper for Foo
that accepts a pointer to a struct and forwards the underlying data pointer, MyStruct->VeryLargeArray
, to Foo
.
MATLAB Code passStruct.m
:
function y = passStruct(x)
%#codegen
coder.cinclude('Foo.h');
s.f = x;
coder.cstructname(s, 'wrapperStruct_T');
y = 10;
y = coder.ceval('WrapFoo', coder.ref(s));
Header file Foo.h
:
/* Include generated header to get struct definition */
#include "passStruct.h"
double WrapFoo(wrapperStruct_T *s);
double Foo(double *x);
Source file Foo.c
:
#include "Foo.h"
double WrapFoo(wrapperStruct_T *s)
{
return Foo(s->f);
}
double Foo(double *x)
{
return 2.0*x[0];
}
Codegen command:
codegen passStruct -args zeros(1000) Foo.c -report