reading ASCII data in s-function from a file

2 次查看(过去 30 天)
Hello all,
I am interested in reading ASCII data in s-function from a file. I am trying to read the data [21;77;46;183;202;84;199;198;27;84] from the file datafile.dat. I am trying to display the data in Scope using *z = ssGetOutputPortSignal(S,0); . But when I run the corresponding Model I could see that only first 5 numbers are read i.e. 21;77;46;183;202. Following is the code:
#define S_FUNCTION_NAME sfunread
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <stdio.h>
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return;
}
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 0)) return;
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 10);
ssSetOutputPortDataType(S,0,SS_SINGLE);
ssSetNumPWork(S,1);
ssSetNumSampleTimes(S, 1);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, 1.0);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
void** pwork = ssGetPWork(S);
FILE *datafile;
datafile = fopen("datafile.dat","r");
pwork[0] = datafile;
}
#endif /* MDL_START */
static void mdlOutputs(SimStruct *S, int_T tid)
{
int l;
real_T *z = ssGetOutputPortSignal(S,0);
void** pwork = ssGetPWork(S);
for (l=0;l<10;l++){
real_T temp;
fscanf(pwork[0],"%f",&temp);
z[l]=temp;
}
static void mdlTerminate(SimStruct *S)
{
void** pwork = ssGetPWork(S);
FILE *datafile;
datafile = pwork[0];
fclose(datafile);
}
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
Please let me know if I am using the correct way to read the ASCII data from a file.
Tushar
  2 个评论
Kaustubha Govind
Kaustubha Govind 2011-4-7
How is the data in your file delimited? Are there spaces between the numbers?
Tushar
Tushar 2011-4-11
The delimiter is next line character '\n'. Each element of the array is on the next line.

请先登录,再进行评论。

回答(2 个)

Jarrod Rivituso
Jarrod Rivituso 2011-4-7
Your overall approach looks good. I would suggest debugging the S-function so you can step through it. There are steps on how to do that here:
Also, did you copy and paste that code directly? There seems to be a missing } to end your for loop. I was looking for something subtle in your code and noticed that (I doubt that is the problem, but it makes me suspicious of whether this code actually contains the problem).

Tushar
Tushar 2011-4-13
Hello
I tried to debug my s-function in Visual C. For simplicity I tried to read array with 5 elements [21;77;46;183;202] from a file. But the values of the elements in z array in the above code are displayed like
5.442276802756e-315#DEN
5.520634191278e-315#DEN
5.488902686835e-315#DEN
5.571469356558e-315#DEN
5.577621382930e-315#DEN
in stead of the true values [21;77;46;183;202]. Please some one comment on this.
  5 个评论
Tushar
Tushar 2011-4-15
Thanks Jarrod. The code runs correctly with real32_T.
Its not clear why I had to change to real32_T from real_T. What is the difference between real_T, real32_T and real64_T? I did not find any reference for these data types in Matlab help files. real_T does not work even for integer type data in the file. Also "%lf" could not read the data from the file.
Jarrod Rivituso
Jarrod Rivituso 2011-4-17
I think for most 32-bit machines the mappings from Simulink S-function typedefs to the actual C data types goes as
real32_t - float
real64_T - double
real_T - double
So, real_T shouldn't work for integer type data.

请先登录,再进行评论。

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by