S functions. Creating a Simulink Block, failing at returning a pointer to an array (S-functions)

11 views (last 30 days)
Hi,
I´m trying to generate an S-function from a C file. It implements a moving average function. It has two inputs: the initial array and the average window. It ouputs a pointer to an array (with different size). I´ve tried with the legacy code tool and the S-function builder. Apparently, with the former it is not possible to deal with pointers? FYI, as a compiler I use Windows SDK 7.1.
First I include what I did in both versions, the M-file for the Legacy Code Tool and the S-function builder output pane (where I think is the error).
**************************** 1st version -> (Legacy Code Tool) ****************************
def = legacy_code('initialize')
def.SourceFiles = {'moving_sum_fcn_no_size_fin_v2.c'};
def.HeaderFiles = {'moving_sum_fcn_no_size_fin_v2.h'};
def.SFunctionName = 'ex_sfun_moving_sum';
def.OutputFcnSpec = 'moving_sum(double u1[1], uint8 u2, double y1[1])';
legacy_code('sfcn_cmex_generate', def)
legacy_code('compile', def)
legacy_code('sfcn_tlc_generate', def)
legacy_code('slblock_generate', def)
**************************** 2nd version -> S-function builder ****************************
// Output pane code: y[0] = moving_sum(u[0],n_avg[0]);
************************* // I receive the following error(s) when compiling it with both functions
ex_moving_sum_wrapper.c ex_moving_sum_wrapper.c(83) : error C2440: 'function' : cannot convert from 'const real_T' to 'double *'
ex_moving_sum_wrapper.c(83) : warning C4024: 'moving_sum' : different types for formal and actual parameter 1
ex_moving_sum_wrapper.c(83) : error C2440: '=' : cannot convert from 'double *' to 'real_T'
************************* Here is the C code *************************
double *calc_sum (double pr[], double b[], static unsigned int arrsize, static unsigned int n_avg)
{
static int k, m;
double s;
double *x;
x = (double*) b;
s = 0;
for (k = 0; k < n_avg; k++)
s = s + *(pr+k);
x[0] = s;
for (k = 0, m = n_avg; m < arrsize; k++, m++)
{
s = s + *(pr+m) - *(pr+k);
x[k+1] = s;
}
return x;
}
double *moving_sum(double ptr[], static unsigned int n_avg)
{
double *b;
static unsigned int size = 3999;
b = (double*) malloc((size-n_avg+1) * sizeof(double));
b = calc_sum(ptr, b, size, n_avg);
return b;
}
*************************
Is the problem related to the compiler? It seems to work with integers instead of double. Are these two options equivalent when dealing with pointers to arrays?
Thanks a lot. Sincerely Jesús.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 9 May 2013
I think you need to re-write moving_sum as the following:
void moving_sum(double ptr[], static unsigned int n_avg, double *b)
{
static unsigned int size = 3999;
calc_sum(ptr, b, size, n_avg);
}
Simulink already allocates the memory for the output signals, so you don't need to call malloc, but just assume that 'b' has the correct amount of memory allocated (for this, you need to set-up the size, datatype and complexity of your output correctly).
In S-function Builder, you need to say something like:
moving_sum(u0, n_avg, y0);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!