title: Return values from a C++ MEX file to MATLAB
body: <p>I am coding a MATLAB program that retrieves data from C++ code. To do so, I have created a MEX-file in MATLAB and a gateway mexFunction.
  Although the read value can be read in MATLAB, I cannot retrieve it to work with it. If this is unclear, I have quite the same problem as here (<a href="https://stackoverflow.com/questions/5943953/how-to-return-a-float-value-from-a-mex-function-and-how-to-retrieve-it-from-m-f">How to return a float value from a mex function, and how to retrieve it from m-file?</a>) but I want to retrieve a value displayed by my C++ program.
  Here is my C++ code:</p>
  
  <pre><code>#define _AFXDLL
  #define  _tprintf mexPrintf
  #include "StdAfx.h"
  #include "704IO.h"
  #include "Test704.h"
  #include "mex.h"
  #ifdef _DEBUG
    #define new DEBUG_NEW
  #endif
  /////////////////////////////////////////////////////////////////////////////
  
  CWinApp theApp;  // The one and only application object
  
  /////////////////////////////////////////////////////////////////////////////
  
  using namespace std;
  
  /////////////////////////////////////////////////////////////////////////////
  int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
  //void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  {
  HMODULE hModule(::GetModuleHandle(NULL));
  short   valueRead;
  
    if (hModule != NULL)
    {
      // Initialize MFC and print and error on failure
      if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
      {
        //mexPrintf("Fatal Error: MFC initialization failed");
        //nRetCode = 1;
      }
      else
      {
          valueRead = PortRead(1, 780, -1);
          mexPrintf("Value Read = %i\n",valueRead);
      }
    }
    else
    {
      _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
    }
    //return nRetCode;
  }
  
  /////////////////////////////////////////////////////////////////////////////
  void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  {
      _tmain(0,0,0);
      return;
  }
  </code></pre>
  
  <p>Please tell me if you need any further information about my issue?</p>