Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Matlab with C++

Status
Not open for further replies.

CusterJess

Computer
Apr 26, 2010
9
Hi there.

I'm working on a project in C++. The thing is that all the math part of it I'm doing in Matlab, that I find so much easier on those aspects.

I'm not quite sure how can I incorporate the Matlab code into the C++ program, though. I readed about creating a dll library, but it was really vague information. Can you guide me through the main steps?

Thanks
 
Replies continue below

Recommended for you

Do you have the Matlab C compiler?

TTFN

FAQ731-376
 
The standard compiler I believe is LCC

[peace]
Fe
 
I think the question was calling Matlab functions from a standalone C++ program. This is the other way around to the common use of C-MEX functions by Matlab. The mechanism is the Matlab Engine. I've not used it myself, but have seen several threads in another forum describing it. Here is a link:


Don't get confused by the term "Matlab Compiler", this is a deployment tool.

Matlab does ship with LCC, but be aware that this is a C compiler and will not build C++ programs.

- Steve
 
Great. I'll go through the compiler documentation and see if I get it clear.

Thanks!
 
Hey, I have the Matlab Compiler, and there's many options. The one I want is buiding a C++ library.

Apparently I have to use the command MCC, and in its help it says :

MCC MATLAB to C/C++ Compiler (Version 4.0).
MCC [-options] fun [fun2 ...]

OPTIONS:

B <filename>[:<arg>[,<arg>]] Specify bundle file. <filename> is a text file
containing Compiler command line options. The Compiler behaves as if the
"-B <filename>" were replaced by the contents of the bundle file. Newlines
appearing in these files are allowed and are treated as whitespace.
The MathWorks provides options files for the following:


cpplib Used for building a C++ library

The thing is that it doesn't work that way, or maybe I'm missing something.

Attached is the matlab code I want to convert to C++.

Thanks
 
 http://files.engineering.com/getfile.aspx?folder=0e14f8c0-95aa-4584-9a7e-554cef5d7d95&file=matlab_code.txt
There is no built in function (in a library) that does FFT for C++, at least I know of none.
You need to write your own C++ code that does FFT. Do it in discrete form. I'd give you the one I wrote for this, but then I'd have to "sue" you. [wink]
It's best if you learn this by doing it, so to speak.


[peace]
Fe
 
I'm still not clear on the original requirement.

a) Convert the Matlab code into equivalent C++ and incorporate that C++ into your program.

b) Retain your Matlab code, but somehow call it from your C++ program.

c) Other.

===

a) The "Matlab compiler" will not help you here. You will almost certainly need to either rewrite the math code in C++. There are some free Matrix class libraries out there. Or you could write your own. I wrote my own when I needed to convert someone elses Matlab into C++. It's educational.

b) As I said, the Matlab Engine is designed for this. It will consume a Matlab license when it runs though of course.

c) ??

- Steve
 
I don't know, I readed that in many places, that you could build a C++ library with the Matlab compiler, using matlab code... but yeah, anyway, I guess I'll do it all in C++. Doing the fft in C++ is not a problem, I have it already, the thing is that I have a lot of data from different tests I did with Matlab and now I'll have to do it all again.

Thanks people!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor