Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

read values from ssGetPWork(S)[0]

$
0
0
I'm having problem in reading the values of a variable whose address is stored in the pointer ssGetPWork(S)[0]. The code for mdlStart and mdlOutput function is as follow

 static void mdlStart(SimStruct *S)
  {
    // declare and initialize one floating variable
    float number3 = 10;
    //
    float *number4;
    /* Allocate memory for number2 and initalize it to zero*/
    ssGetPWork(S)[0] = &number3;
    number4 = ssGetPWork(S)[0];
    // store the pointer to filename in ssGetPWork(S)[1]
    printf("ssGetPWork(S)[0] %f\n",number4[0]);
    printf("ssGetPWork(S)[0] %f\n",ssGetPWork(S)[0]);
  }
#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
    InputRealPtrsType u = ssGetInputPortRealSignalPtrs(S,0);
    float *number = ssGetPWork(S)[0];
    // read the input values (Here, 2 elements are taken from mux block)
    double clk= *u[0];
    double y = *u[1];
// number = ssGetPWork(S)[0];
      if (clk!=0 && fmod(clk,120)==0)//(clk/10==0)
       { // increment counter value
     printf("ssGetPWork(S)[0] %f\n",number[0]);
     printf("ssGetPWork(S)[0] %f\n",ssGetPWorkValue(S,0));
       }
}

When I run this C Mex S Function, I got the following output

ssGetPWork(S)[0] 10.000000
ssGetPWork(S)[0] 0.000000
ssGetPWork(S)[0] 0.000000
ssGetPWork(S)[0] 0.000000
Now what I can't understand is:
Why this printf function printf("ssGetPWork(S)[0] %f\n",number4[0]); displays correct values, whereas directly using ssGetPWork[0] does not display correct values i.e.,
 printf("ssGetPWork(S)[0] %f\n",ssGetPWork(S)[0]);

Furthermore, when the values of ssGetPWork(S)[0] is displayed in the mdlOutputs block, then I do not get initialized values, instead I got 0. I can't understand where is the problem in the code.

Thanks in advance

Viewing all articles
Browse latest Browse all 19628

Trending Articles