I’ve used the fscanf() function in the past with no problems, but for some reason, my current use of it in my C-mex s-function (Matlab-compatible C code) is causing problems. Below is a simplified version of my code (note that the mdlStart() function in a c-mex s-function is analogous to the main() function in C):
Code:
#define NUM_TARGS 20
double targets[NUM_TARGS];
static void mdlStart(SimStruct *S){
int i = 0;
FILE *in1;
in1 = fopen(“target_vals.txt”, “r”);
/* initialize the array to 0.0 */
for(i = 0; i < NUM_TARGS; i++)
targets[i] = 0.0;
/* read in values from input file to targets[ ] array */
for(i = 0; i < NUM_TARGS; i++){
fscanf(in1, “%f”, &targets[i]);
mexPrintf("targets[%d] = %f.\n", i, targets[i]); /* each targets[] value prints as 0.0 !! */
}
} /* end mdlStart() */
The format of the target_vals.txt file is a list of 20 double values, each on its own line (i.e. a newline character after each value).
The problem: when I print the values being read into the targets[ ] array, they all display as 0.0, when the actual values contained in this file are all positive-valued doubles. I’ve also tried reading in the “%lf” type in, but that hasn’t resulted in any improvements.
Any suggestions about what’s wrong with my code above, that’s causing it to assign all 0 values to the targets[ ] array (or maybe, never read in values from the input file at all??), instead of reading in the correct double values from the input file? I’ve read through various tutorials and examples, but I haven’t been able to recognize the problem yet.
Thanks for your help.