0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

Although the solution proposed by lilbill39 will work, I came across a different approach:

function y = passStruct(x)
%#codegen
s.f = x;
ps = coder.opaque('double *','&s.f[0]');
y = 0;
y = coder.ceval('Foo', ps);

However, it assumes that the name of the struct is not changed in the code generation process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.