-2

I want to convert my Matlab code to c++. I did search for it but I got more confused. Could anyone show me the simplest way to do that? If any information about code is needed please let me know. Any help is highly appreciated.

Oliver Range
  • 365
  • 3
  • 10
  • 2
    Matlab is a specialized language with a huge library of built-in functions. C++ is a general-purpose programming language *that does not have those functions*. There is no automatic conversion to plain C++. What you need to do is look for a math library for C++ that does what you want then learn to use it. – Dave S Jun 03 '17 at 18:43
  • But I have heard that there are some ways to convert Matlab code to c++. as Matlab coder...@DaveS – Oliver Range Jun 03 '17 at 18:45
  • What have you heard? What does a Google search tell you? – Dave S Jun 03 '17 at 18:48
  • http://www.mathworks.com/help/simulink/ug/functions-supported-for-code-generation--alphabetical-list.html It says that it is possible for some functions @DaveS – Oliver Range Jun 03 '17 at 18:58
  • If you pay enough, Matlab has a utility that cranks out C code. It's actual value depends on what you want to do with it. – user4581301 Jun 03 '17 at 18:59
  • What about c++? @user4581301 – Oliver Range Jun 03 '17 at 19:00
  • You pray the C code integrates well with your C++ code. – user4581301 Jun 03 '17 at 19:01
  • Isn't there any free choice? Or any software to do that?@user4581301 – Oliver Range Jun 03 '17 at 19:01
  • Is this link related to what you say about paying matlab? @user4581301 – Oliver Range Jun 03 '17 at 19:05
  • See http://www.mathworks.com/help/fixedpoint/ug/what-are-code-acceleration-and-code-generation-from-matlab.html and then "C Code Generation at the Command Line (MATLAB Coder)" – Dave S Jun 03 '17 at 19:07
  • I am not sure if there is an easy way to do this except with [Matlab Coder](https://www.mathworks.com/products/matlab-coder.html). You can write your own mexFunction and compile it in Matlab. Check out this [answer](https://stackoverflow.com/questions/43903860/how-to-compare-results-between-c-and-matlab/43912992#43912992). – kedarps Jun 03 '17 at 19:08
  • One more question: Is it less time consuming to run a code in c++ than matlab?@kedarps – Oliver Range Jun 03 '17 at 19:12
  • Do you answer for above question? @user4581301 – Oliver Range Jun 03 '17 at 19:13
  • Too many variables to answer that, I'm afraid. You can write awesome or crappy code in either. What is your goal? Produce an executable that does not require any bits and pieces of Matlab so you can move it around to other, Matlab-less computers? Raw performance? – user4581301 Jun 03 '17 at 19:18
  • I have written my code in matlab. The code is really slow. So I decide to change it to c++ code to make it faster. I want to understand if it really makes the code faster to run or not @user4581301 – Oliver Range Jun 03 '17 at 19:22
  • Might be worth finding out why the Matlab code is really slow because if the algorithm is just plain slow, porting it to C++ won't help. – user4581301 Jun 03 '17 at 19:34
  • Then why do people prefer to write heavy simulations in c++? @user4581301 – Oliver Range Jun 03 '17 at 19:39
  • What I'm saying is if the algorithm is slow in Matlab it will be slow in C++. On the other hand if you implemented a fast algorithm in a manner that is inefficient in Matlab, you may be able to fix up the Matlab code to take advantage of what Matlab does well. – user4581301 Jun 03 '17 at 19:46

1 Answers1

0

Matlab has a tool that converts your Matlab .m to .mex and may also generate a C/C++ code, see https://www.mathworks.com/examples/matlab-coder

Claes Rolen
  • 1,446
  • 1
  • 9
  • 21
  • There is no c++ in the link – Oliver Range Jun 03 '17 at 18:59
  • The page you linked to said "C/C++" because C++ compilers also compile C. That page links to this page http://www.mathworks.com/help/fixedpoint/ug/what-are-code-acceleration-and-code-generation-from-matlab.html which links to "C Code Generation at the Command Line (MATLAB Coder)" – Dave S Jun 03 '17 at 19:06
  • "C++ compilers also compile C" Mostly. For a quick example of when they don't watch what happens with the `auto` keyword. – user4581301 Jun 03 '17 at 19:07
  • From the 'codegen' documentation ... If you specify C++, MATLAB Coder™ wraps the C code into .cpp files so that you can use a C++ compiler and interface with external C++ applications. It does not generate C++ classes. – Claes Rolen Jun 03 '17 at 19:16