Jeremy W. Langston

Personal Website

Steps for using MATLAB C/C++ Compiler with Visual C++

In Visual Studio:

0)  Create an empty C/C++ project.

 
In MATLAB:

1)  Write the m-file with one function to expose to C/C++.

        function [xp, length] = ExpandArray( x, interval)
        xp = 0:interval:x(length(x));
        length = length(xp);

2)  Create a deployment project (File->New->Deployment Project).

3)  Add m-file to “Exported functions” in deployment project.

4)  Change the settings of the deployment project to put the output directory output to the directory of the source files for the C/C++ project.  Set the Library Name (e.g. “Functions”).
In Visual Studio:

5)  Right-click on the project->Properties.

5a) Under C/C++->General, add the directory of the MATLAB header files to “Additional Include Directories”:

      "C:\Program Files\MATLAB\R2007a\extern\include"

5b) Under Linker->Input, add the directory of the MATLAB libraries and the location of your newly created MATLAB library to “Additional Dependencies”:

      "C:\Program Files\MATLAB\R2007a\extern\lib\win32\microsoft\mclmcrrt.lib"
      "C:\...\Functions.lib", where "..." is the path of the output directory set in step 4

6)  Add the created header file to your project and include it in your C/C++ code where appropriate.

7)  In your C/C++ code, initialize the MATLAB component:

      bool ret = FunctionsInitialize();
      if (!ret){
        std::cout << "Error initializing MATLAB Component Runtime\n";
        system("PAUSE");
        return 0;
      }

8)  Create the input variables to the MATLAB function:

      double x[4] = {1,2,3,4};
      mwArray mwX(1,4,mxDOUBLE_CLASS);
      mwX.SetData(x,4);
      mwArray mwInterval((double) 0.1);
      int nargout = 2; // this says you will be using 2 outputs, xp and length

9)  Create the output variables from the MATLAB function:

      mwArray mwXP;
      mwArray mwLength;

10) Call your function:

      ExpandArray(nargout, mwXP, mwLength, mwX, mwInterval);

11) Get the data out of the mwArrays:

      int length;
      mwLength.GetData(&length, 1);
      double *xp = new double[length];
      mwXP.GetData(xp, length);

12) Terminate the use of the MATLAB component:

      FunctionsTerminate();

 
Notes:

If you only want to return one variable from your MATLAB function, set
nargout to 1 and the return variable will be as such:
  int returnVariable = MyFunction(nargout, inputVariable);

14 Comments

  1. Thanks for sharing informative information keep sharing with us. Subway Listens

  2. What a fabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.

  3. I am just looking to search Tom Hardy Leather Coat Bane and found the nice websites offering good discount! thanks

  4. I am immensely appreciative for the immeasurable time and profound insight you have bestowed upon me. I highly value the impact you have on me. gacha life game

  5. I have been practicing C++ since 2 years

  6. I think the experts might give a good idea or suggestion about it.
    slot5000

  7. Thank you very much dear editor thist dear article

  8. In papa’s pizzeria, you play as Roy, an unsuspecting delivery boy who suddenly finds himself in charge of a bustling pizzeria after the owner, Papa Louie, leaves town. With no prior experience, Roy must take on the challenge of running the entire operation, from taking orders to baking pizzas and serving customers. The game is set in a colorful, cartoonish world where customer satisfaction is key to success.

  9. Very helpful code …
    Thank you for sharing

  10. Great Article. You have beautifully articulated it. Readers revisit only if they find something useful. halloween bomber jacket

  11. I’m definitely book marking this for the future when I rebrand! A really helpful post thank you!

  12. I’m definitely book marking this for the future when I rebrand! A really helpful post thank you!.

  13. not even those angry rants left in caps lock on social media. Those are dark times, my friends.
    ahha4d

Leave a Reply

Your email address will not be published.

© 2024 Jeremy W. Langston

Theme by Anders NorenUp ↑