0

I created a user defined block with single input and single output in simulink and it works perfectly fine. But when i create a user defined block without an input, run this block in a simulink model, MATLAB Crashes. Below is the code for the C S-function.

Any information regarding this is appreciated.


#define S_FUNCTION_NAME My5times /* Defines and Includes */
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"

static void mdlInitializeSizes(SimStruct *S)
{
     ssSetNumSFcnParams(S, 2);    
     if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))     
     {    
        return; /* Parameter mismatch reported by the Simulink engine*/    
     }

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

    if (!ssSetNumOutputPorts(S,1)) 
        return;
    ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
    ssSetNumSampleTimes(S, 1);

    /* Take care when specifying exception free code - see sfuntmpl.doc */    
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);    
}

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

static void mdlOutputs(SimStruct *S, int_T tid)    
{    
    int_T i;    

    /*int_T param = mxGetScalar(ssGetSFcnParam(S,0));*/        
    int_T buflen = mxGetN(ssGetSFcnParam(S, 0)) * sizeof(mxChar) + 1;    
    char_T* StringParam = mxMalloc(buflen);     
    int_T status = mxGetString(ssGetSFcnParam(S, 0), StringParam, buflen); 

    /* mexPrintf("The string being passed as a Paramater is - %s\n ", String);*/
    /*  InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S, 0);*/

    real_T *y = ssGetOutputPortRealSignal(S, 0); 
    int_T width = ssGetOutputPortWidth(S, 0);    
    for (i = 0 ; i < width ; i++)     
    {    
        *y++ = 1;    
    }
}

static void mdlTerminate(SimStruct *S){}    

#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */    
#else    
#include "cg_sfun.h" /* Code generation registration function */    
#endif
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Be_bro
  • 15
  • 1
  • 5
  • 2
    Why people format their code in such an ugly way? why do I cry every time I see code so badly formatted? why do you add an empty line after each line? – Iharob Al Asimi Jan 29 '15 at 16:05
  • Probably `mxGetString` includes the terminating `'\0'` so you are requesting `1` extra character? – Iharob Al Asimi Jan 29 '15 at 16:08
  • Hi, Thanks for formatting the code though. The entire code is working perfectly fine if the number of input port is non zero. Only when I set the input port to 0, it crashes. SO I dont think there is any problem with the instruction 'mxGetString'. I believe the issue has something to do with input port instructions. – Be_bro Jan 29 '15 at 16:23
  • What is ne number of input port? – Iharob Al Asimi Jan 29 '15 at 16:24

1 Answers1

1

Why do you call the following two functions after setting the number of inputs to 0?

ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 0);

These lines are setting the first input port which you do not have.

Navan
  • 4,407
  • 1
  • 24
  • 26