c - 在 simulink 中运行用户定义的 block 时 Matlab 崩溃

标签 c matlab simulink s-function rtw

我在 simulink 中创建了一个具有单输入和单输出的用户定义 block ,它工作得很好。但是,当我创建一个没有输入的用户定义 block 时,在 simulink 模型中运行该 block 时,MATLAB 崩溃。下面是 C S 函数的代码。

任何与此相关的信息都值得赞赏。

<小时/>
#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

最佳答案

为什么将输入数量设置为0后还要调用以下两个函数?

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

这些行正在设置您没有的第一个输入端口。

关于c - 在 simulink 中运行用户定义的 block 时 Matlab 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218943/

相关文章:

c - 将stm32 ADC值写入SD卡

c - C中的读/写函数

通过C程序进行Android串口通信

matlab - 在 Matlab 中将结构转换为 double 类型

MATLAB - 如何声明一个 "row slots"的矩阵供我插入行?

Matlab to workspace 和 rms 值不同

eclipse - Eclipse中的多个 “could not be resolved”

c - C 中的 qsort() 用于 char 指针数组

matlab - 如何在基于 Matlab 中的二进制掩码标记的 ROI 上应用图像处理算法?

matlab - Simulink 项目依赖管理和依赖解析