我尝试使用 fopen() 在 Simulink s-function 中打开一个文件,但是当我添加必要的行时它不会编译。这是错误日志:
timestwo.c(84) : error C2275: 'int_T' : illegal use of this type as an expression
C:\Program Files (x86)\MATLAB\R2011bSP1\extern\include\tmwtypes.h(533) : see declaration of 'int_T'
timestwo.c(84) : error C2146: syntax error : missing ';' before identifier 'i'
timestwo.c(84) : error C2065: 'i' : undeclared identifier
timestwo.c(85) : error C2275: 'InputRealPtrsType' : illegal use of this type as an expression
c:\program files (x86)\matlab\r2011bsp1\simulink\include\simstruc.h(897) : see declaration of 'InputRealPtrsType'
timestwo.c(85) : error C2146: syntax error : missing ';' before identifier 'uPtrs'
timestwo.c(85) : error C2065: 'uPtrs' : undeclared identifier
timestwo.c(85) : warning C4047: '=' : 'int' differs in levels of indirection from 'InputRealPtrsType'
timestwo.c(86) : error C2275: 'real_T' : illegal use of this type as an expression
C:\Program Files (x86)\MATLAB\R2011bSP1\extern\include\tmwtypes.h(500) : see declaration of 'real_T'
timestwo.c(86) : error C2065: 'y' : undeclared identifier
timestwo.c(87) : error C2275: 'int_T' : illegal use of this type as an expression
C:\Program Files (x86)\MATLAB\R2011bSP1\extern\include\tmwtypes.h(533) : see declaration of 'int_T'
timestwo.c(87) : error C2146: syntax error : missing ';' before identifier 'width'
timestwo.c(87) : error C2065: 'width' : undeclared identifier
timestwo.c(89) : error C2065: 'i' : undeclared identifier
timestwo.c(89) : error C2065: 'i' : undeclared identifier
timestwo.c(89) : error C2065: 'width' : undeclared identifier
timestwo.c(89) : error C2065: 'i' : undeclared identifier
timestwo.c(95) : error C2065: 'y' : undeclared identifier
timestwo.c(95) : error C2100: illegal indirection
timestwo.c(95) : error C2065: 'uPtrs' : undeclared identifier
timestwo.c(95) : error C2065: 'i' : undeclared identifier
timestwo.c(95) : error C2109: subscript requires array or pointer type
这是代码。这只是 Simulink 中将输入向量乘以 2 (timestwo.c) 的演示代码。我猜 stdio.h 和 stdlib.h 的包含导致错误,但我不确定。我可以做些什么来在 S-Function 中使用 fopen 吗?
/*
* File : timestwo.c
* Abstract:
* An example C-file S-function for multiplying an input by 2,
* y = 2*u
*
* Real-Time Workshop note:
* This file can be used as is (noninlined) with the Real-Time Workshop
* C rapid prototyping targets, or it can be inlined using the Target
* Language Compiler technology and used with any target. See
* matlabroot/toolbox/simulink/blocks/tlc_c/timestwo.tlc
* the TLC code to inline the S-function.
*
* See simulink/src/sfuntmpl_doc.c
*
* Copyright 1990-2009 The MathWorks, Inc.
* $Revision: 1.1.6.2 $
*/
#define S_FUNCTION_NAME timestwo
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <stdio.h>
#include <stdlib.h>
/*================*
* Build checking *
*================*/
/* Function: mdlInitializeSizes ===============================================
* Abstract:
* Setup sizes of the various vectors.
*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetNumSampleTimes(S, 1);
/* specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
/* Take care when specifying exception free code - see sfuntmpl_doc.c */
ssSetOptions(S,
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR);
}
/* Function: mdlInitializeSampleTimes =========================================
* Abstract:
* Specifiy that we inherit our sample time from the driving block.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
/* Function: mdlOutputs =======================================================
* Abstract:
* y = 2*u
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
FILE * datei;
datei = fopen("Test.txt","w");
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);
for (i=0; i<width; i++) {
/*
* This example does not implement complex signal handling.
* To find out see an example about how to handle complex signal in
* S-function, see sdotproduct.c for details.
*/
*y++ = 2.0 *(*uPtrs[i]);
//fputc(2,datei);
}
}
/* Function: mdlTerminate =====================================================
* Abstract:
* No termination needed, but we are required to have this routine.
*/
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