文件

编写非束缚的S函数

非束缚的S函数是由Simulink对待的C或C ++ MEX S函数金宝app®引擎和由生成的代码。您根据S函数API实现算法一次。Simu金宝applink引擎和生成的代码调用S函数例程(例如,mdloutputs.)在模型执行期间。

不束缚的S函数被缺失识别答案。tlc文件为您的s函数。文件名根据您的平台而异。例如,在64位微软®Windows®系统,文件名是答案mexw64。在matlab.®Command Window, typeMexext.查看系统使用哪个扩展。

编写非束缚S函数的指南

  • MEX文件无法调用MATLAB函数。

  • If the MEX-file uses functions in the MATLAB External Interface libraries, include the header filecg_sfun.h.代替mex.hor金宝appsimulink.c.。对于标题文件cg_sfun.h.,在S函数结束时,包括这些行:

    #ifdef matlab_mex_file / *这个文件是被编译为mex文件的吗?* / #include“金宝appsimulink.c”/ * mex文件界面机制* / #else #include“cg_sfun.h”/ *代码生成注册函数* / #endif
  • 仅使用代码生成器支持的MATLAB API函数。金宝app支持的A金宝appPI函数是:

    • mxgeteps.

    • mxGetInf

    • mxgetm.

    • mxgetn.

    • mxgetnan.

    • mxGetPr

    • mxgetscalar.

    • mxgetstring.

    • mxisusempty.

    • mxisfinite.

    • mxisinf.

  • MEX library calls are not supported in generated code. To use such calls in the MEX-file and not in the generated code, add the following condition:

    #ifdef MATLAB_MEX_FILE #endif
  • 仅使用仅包含实际数据的完整矩阵。

  • 请勿指定呼叫的返回值mxgetstring.。如果要指定返回值,MEX文件不会编译。相反,使用函数的第二个输入参数,该参数返回指向字符向量的指针。

  • 使用正确的#定义s-function_name.陈述。您指定的S函数名称必须匹配S函数文件名。

  • 如果可能,请使用数据类型real_Tandint_T代替双倍的andint。The data typesreal_Tandint_Tare more generic and can be used in multiple environments.

  • 提供构建过程,其中包含用于构建S函数的模块的名称。使用模板制作文件,set_param.功能,或S函数模块田野S函数block parameters dialog box. For example, suppose that you build your S-function with this command:

    Mex sfun_main.c sfun_module1.c sfun_module2.c

    You can then use the following call toset_param.to include the required modules:

    set_param(sfun_block,“SfunctionModules”“sfun_module1 sfun_module2”的)

    当您准备好生成代码时,强制代码生成器重建顶部模型。有关更多信息,请参阅顶级模型代码的控制再生(金宝appSimulink编码器)。

Noninlined S-Function Parameter Type Limitations

Parameters to noninlined S-functions can be of the following types only:

  • 双精度

  • 标量中的字符,矢量或2-D矩阵

有关可以在S函数或S函数中提供的参数类型的更灵活性,请在内联函数并考虑使用mdlrtw.S函数例程。

Use of other functions from the MATLABmatrix.hAPI or other MATLAB APIs, such asmex.hand数学,不受支持。金宝app如果从S函数源文件中呼叫不受金宝app支持的API,则会发生编译器错误。有关支持的MATLAB API金宝app函数的详细信息,请参阅文件matlabroot/ RTW / C / SRC /RT_MATRX.H.andmatlabroot/ RTW / C / SRC /RT_MATRX.C.

如果你使用mxGetPr在空矩阵上,该功能不会返回NULL。It returns a random value. Therefore, you must protect calls tomxGetPr通过使用mxisusempty.

Related Topics