What am i doing wrong with my simple C S-Function??

What am i doing wrong with my simple C S-Function??

Post by Rober » Sun, 12 Sep 2004 01:42:08


Hi!
I would like to create a C S-Function that can take two vectors as inputs,
and these vectors can have any size independently of each other. Also, the
output is a matrix with the number of rows equal to the length of first
input vector, and the number of columns equal to the second input vectors
length. But when i try to implement this as a S-Function, and call it from
a S-function block Simulink complains about the input corresponding to the
vector with the most elements. I guess this is because Simulink for some
reason still assumes this to have the same size as the shorter vector, but i
am not very experienced with writing S-Functions so i dont know. If anybody
could help me i would be very grateful.


/Robert

Here is the actual C-file for the S-function:
################################


#define S_FUNCTION_NAME VMult
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"

static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return;
}

if (!ssSetNumInputPorts(S, 2)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortWidth(S, 1, DYNAMICALLY_SIZED);

ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 1, 1);

if (!ssSetNumOutputPorts(S,1)) return;

ssSetOutputPortMatrixDimensions(S, 0, ssGetInputPortWidth(S, 0),
ssGetInputPortWidth(S, 1));

ssSetNumSampleTimes(S, 1);

ssSetOptions(S,
SS_OPTION_ALLOW_PARTIAL_DIMENSIONS_CALL |
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR);

}


static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
ssSetSampleTime(S, 1, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 1, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}

static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
int_T j;
InputRealPtrsType uPtrs0 = ssGetInputPortRealSignalPtrs(S,0);
InputRealPtrsType uPtrs1 = ssGetInputPortRealSignalPtrs(S,1);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T m = ssGetInputPortWidth(S, 0);
int_T n = ssGetInputPortWidth(S, 1);

for (i=0; i<m; i++) {
for (j=0; j<n; j++) {

y[i][j] = *uPtrs0[i] * *uPtrs1[j];
}
}
}

static void mdlTerminate(SimStruct *S)
{
}


#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif