这可能是一个简单的问题。但我找不到办法解决这个问题。我正在使用s功能块向我的算法输入波形信号。波形信号正在从大约2000点的文件中读取
首先我开始模拟时间等于50.然后它只读取50分。我检查了'tout'变量。这是0,1,2,...... 50
然后我将模拟时间增加到100.结果是相同的。只读50分。但是小号是0,2,4,6..50
我尝试了10000.无论我做什么,它只读取50个值,只有0,200,400,600等宽时间步。 我的s功能或simulink设置有问题吗? 这是c s函数文件
/* Give S-function a name */
#define S_FUNCTION_NAME Readip
#define S_FUNCTION_LEVEL 2
/* Include SimStruct definition and file I/O functions */
#include "simstruc.h"
#include <stdio.h>
#include <stdlib.h>
static FILE* file2;
/* Called at the beginning of the simulation */
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return;
}
if (!ssSetNumOutputPorts(S, 3)) return;
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
ssSetOutputPortWidth(S, 0, 1);
ssSetOutputPortDataType(S,0,DYNAMICALLY_SIZED);
ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
ssSetOutputPortWidth(S, 1, 1);
ssSetOutputPortDataType(S,1,DYNAMICALLY_SIZED);
ssSetOutputPortOptimOpts(S, 1, SS_REUSABLE_AND_LOCAL);
ssSetOutputPortWidth(S, 2, 1);
ssSetOutputPortDataType(S,2,DYNAMICALLY_SIZED);
ssSetOutputPortOptimOpts(S, 2, SS_REUSABLE_AND_LOCAL);
ssSetNumPWork(S,1);
ssSetNumSampleTimes(S, 1);
}
/* Set sample times for the block */
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* Function: mdlStart ========
===============================================
* Abstract:
* This function is called once at start of model execution. If you
* have states that shou
ld be initialized once, this is the place
* to do it.
*/
static void mdlStart(SimStruct *S)
{
/*at start of model execution, open the file and store the pointer
*in the pwork vector */
void** pwork = ssGetPWork(S);
FILE *datafile;
datafile = fopen("table.data","r");
pwork[0] = datafile;
}
#endif /* MDL_START */
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector, ssGetY(S).
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
//get pointer to the block's output signal
real_T *y1 = ssGetOutputPortSignal(S,0);
real_T *y2 = ssGetOutputPortSignal(S,1);
real_T *y3 = ssGetOutputPortSignal(S,2);
char a[10];
char b[10];
char c[10];
/*get pointer to array of pointers, where the first element is the address
*of the open file */
void** pwork = ssGetPWork(S);
/*read a floating point number and then the comma delimiter
*store the result in y*/
fscanf(pwork[0],"%s %s %s",&a,&b,&c);
*y1=atof(a);
*y2=atof(b);
*y3=atof(c);
}
/* Function: mdlTerminate =====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
//close the file
void** pwork = ssGetPWork(S);
FILE *datafile;
datafile = pwork[0];
fclose(datafile);
}
/*=============================*
* Required S-function trailer *
*=============================*/
#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 :(得分:3)
这似乎只是一个设置问题。如果您没有为求解器指定步长,并且块不指示采样时间,则Simulink将选择默认值Simulation Time / 50
。只需打开Model Configuration Parameters对话框,然后单击左侧窗格中的求解器即可。如果使用固定步长求解器,则可以显式设置步长。如果使用变步长求解器,则可以指定最大/最小步长。
此外,如果您希望专门为您的S函数指定一个离散的采样周期,那么您可能需要查看此link以确保您根据需要实施mdlInitializeSampleTimes
答案 1 :(得分:1)
您需要使用固定步长离散解算器。 将Periodic sample time constraint设置为'Unconstrined' 输入固定步长和模拟停止时间,以便获得2000步。
例如1s / step和2000s停止时间,或0.1s /步和200s停止时间。