Azzera filtri
Azzera filtri

Creating C code from matlab with changing data input size.

3 visualizzazioni (ultimi 30 giorni)
Hi all
I wrote a simple program.
function y = test(x) %#codegen
y = x+2;
end
Now I want to create c code and I need to define the type and the length of x. I define x as type double and length of 10;
void test(const real_T x[10], real_T y[10])
{
int32_T i0;
for (i0 = 0; i0 < 10; i0++)
{
y[i0] = x[i0] + 2.0;
}
}
The C code is OK!
How can it work in a case that the length of x is changing ?
Thx

Risposte (2)

Kaustubha Govind
Kaustubha Govind il 21 Ago 2012
I think you need to specify the type of the input using:
>> codegen test -args {coder.typeof(double(0), [1 Inf])}
  1 Commento
zohar
zohar il 21 Ago 2012
Hi Kaustubha, Thank you for your response!
I tried what you suggest and I got error...
??? Computed maximum size is not bounded.
Static memory allocation requires all sizes to be bounded.
The computed size is [1 x :?].
Please consider enabling dynamic memory allocation to allow unbounded sizes.
So I tried this
cfg = coder.config('exe');
cfg.DynamicMemoryAllocation = 'AllVariableSizeArrays';
codegen -config cfg test -args {0}
And I got error
??? Build error: Build failed for project 'test_rtw'. See the target build log for further details.
Error in ==> test Line: 1 Column: 1
Any suggestion ?

Accedi per commentare.


zohar
zohar il 22 Ago 2012
I found the solution.
1) On the MATLAB Coder project Overview tab, select the input parameter that you want to define.
2)Click the Actions icon.
3)From the menu, select 'Define Type'.
4)Click the Actions icon.
5)Select 'Make Sizes Variable'.
6)Open Project settings.
7)In the Dynamic memory allocation select 'For all variable-sized arrays'.
8)Build.
void test(const emxArray_real_T *x, emxArray_real_T *y)
{
int32_T i0;
int32_T loop_ub;
i0 = y->size[0] * y->size[1];
y->size[0] = 1;
y->size[1] = x->size[1];
emxEnsureCapacity((emxArray__common *)y, i0, (int32_T)sizeof(real_T));
loop_ub = x->size[0] * x->size[1] - 1;
for (i0 = 0; i0 <= loop_ub; i0++) {
y->data[i0] = x->data[i0] + 2.0;
}
}
  1 Commento
Kaustubha Govind
Kaustubha Govind il 22 Ago 2012
Great! Thanks for posting your solution! It is likely that my solution will work in newer versions, but I'm not sure.

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB Coder in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by