title: Reading Variable from .mat file in C++
body: <p>I am making a program in which, I am doing some processing in matlab then save the result in .mat file using following command in matlab,</p>
  
  <pre><code>save data x;
  </code></pre>
  
  <p>let suppose value of x=2,</p>
  
  <p>Now I am reading the same data.mat file from C++ program in Visual studio 2010. My program is compiling and I can also read the name of arrays and its dimensions perfectly,.. Now the problem is when I am using following command, I cannot read the exact value of x. It is showing me some random values each time I run the program.</p>
  
  <pre><code>variable = matGetNextVariable(pmat, &amp;name);
  </code></pre>
  
  <p>right now value of variable =50779048..</p>
  
  <p>Kindly guide me where I am making mistakes. the value of variable should be 2, because I have saved 2 from matlab cammand..</p>
  
  <p>I already check this question but it seems nobody answered it, 
  <a href="https://stackoverflow.com/questions/29811815/reading-mat-file-in-c">Reading .mat file in C++</a></p>
  
  <pre><code>void main(int argc, char **argv)
  {
      MATFile *pmat;
      const char* name=NULL;
      mxArray *pa;
  
      /* open mat file and read it's content */
      pmat = matOpen("data.mat", "r");
      if (pmat == NULL) 
      {
          printf("Error Opening File: \"%s\"\n", argv[1]);
          return;
      }
  
      /* Read in each array. */
      pa = matGetNextVariable(pmat, &amp;name);
      while (pa!=NULL)
      {
          /*
          * Diagnose array pa
          */
          printf("\nArray %s has %d dimensions.", name, 
                 mxGetNumberOfDimensions(pa));
  
          //print matrix elements
          printf("\ndata %d",pa);
  
          //get next variable
          pa = matGetNextVariable(pmat,&amp;name);
  
          //printf("\ndata %d",pa);
          //destroy allocated matrix
          mxDestroyArray(pa);
      }
  
      matClose(pmat);
  }
  </code></pre>
  
  <p>Thank you.</p>