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);

24 Comments

  1. The comprehensive and informative guides on sustainable event planning and eco-friendly event practices on your website make it a valuable resource for backlink inclusion among event management companies and event planning resources.Cyberpunk 2077 Samurai Jacket

  2. I have read your blog post and that is really informative as well. My friend recommended your blog and trust me through reading such amazing updates we able to change our perception as well. Keep writing such custom hat amazing stuff always. Star Trek Picard Field Jacket

  3. Thanks for sharing informative information keep sharing with us.

    • Event organisers and planning firms may benefit greatly from linking to your site for the wealth of information drift hunters you give on sustainable event planning and environmentally friendly event practises.

  4. Thanks for sharing such a valuable content.

  5. Gain insights into effective negotiation techniques and strategies for conflict resolution. Cyberpunk 2077 Green Leather Jacket

  6. Embark on a transformative journey fueled by our meticulously curated and thought-provoking content. Cyberpunk 2077 Jacket

  7. Immerse yourself in the deep well of wisdom offered by our meticulously investigated articles. Cyberpunk 2077 Jacket

  8. The article is a valuable addition to the field of knowledge. Kate Upton Jacket

  9. Jean-Luc Picard is known for his distinguished style, often wearing his Starfleet uniform or more casual attire. If a leather jacket were to be introduced in the upcoming season, it might symbolize a more adventurous or unconventional phase in his life. However, without specific information about the jacket’s design or significance in the story, it’s challenging to provide a detailed description.

  10. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking
    brown leather jacket mens

  11. Excellent article. Very interesting to read. I really love to read such a nice article.
    brown leather jacket mens

  12. The web page you have shared contains a lot of information. I have accumulated a lot of information.

  13. You really work great for this post. Thank you for sharing!Immigration Fraud Lawyer

  14. Thank you for sharing this insightful article! It provided me with a fresh perspective on the topic and expanded my understanding. Need Writing Help Visit now

  15. Thank you for sharing this insightful article! It provided me with a fresh perspective on the topic and expanded my understanding. Need Writing Help Visit now https://myassignmenthelp.com/

  16. I found this post very interesting and informative. Thank you for sharing your special thoughts with us. I definitely share this with my peeps.

  17. I found this very interesting and informative post. Thanks for sharing this with us.

  18. I value this blog post very interesting.

  19. As dedicated A4 Paper Manufacturers, we take pride in producing high-quality paper products that meet the diverse needs of businesses, offices, and individuals worldwide. Our commitment to excellence ensures that our A4 paper is not only reliable but also environmentally responsible.

  20. Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though.

Leave a Reply

Your email address will not be published.

© 2023 Jeremy W. Langston

Theme by Anders NorenUp ↑