Use Matlab data structures in C++? - c++

I am currently working on a project in C++, and I am actually interested in using Matlab data structures, instead of having to create my own data types (such as matrices, arrays, etc.)
Is there a way to seamlessly use Matlab objects in C++? I don't mind having to run Matlab in the background while my program runs.
EDIT: A starting point is this: http://www.mathworks.co.uk/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html. I will continue reading this.

You can use instead Armadillo C++ maths library; used by NASA, Boeing, Siemens, Deutsche Bank, MIT, CMU, Stanford, etc.
They have good documentation and examples if you are more familiar with MATLAB/OCTAVE
http://arma.sourceforge.net/docs.html#syntax
I would prefer using native C++ library of some sort and not Matlab. This is likely to be faster for both development and execution.

From writing C++ extensions for Matlab I learned one thing: Using Matlab objects in C++ is likely to give you considerable headache.
Matlab data structures are not exposed as C++ classes. Instead, you get pointers that you can manipulate with C-like API functions.
I recommend to use a native C++ library such as Eigen3.

The functionality you are looking at is not really intended to be used as seamless objects. In the past when I have used it I found it much simpler to do the C parts using either native arrays or a third party matrix library and then convert it into a Matlab matrix to return.
Mixing Matlab and C++ is typically done in one of two ways:
Having a C++ program call Matlab to do some specialist processing. This is chiefly useful for rapid development of complex matrix algorithms. You can do this either by calling the full Matlab engine, or by packaging you snippet of Matlab code into a shared library for distribution. (The distributed version packages a distributable copy of the Matlab runtime which is called with your scripts).
Having a Matlab script call a C++ function to do some specialist processing. This is often used to embed C++ implementations of algorithms (such as machine learning models) or to handle specific optimizations.
Both of these use cases have some overhead transferring the data to/from Matlab.
If you are simply looking for some matrix code to use in C++ you would be better off looking into the various C++ matrix libraries, such as the one implemented in Boost.

You can do mixed programming with C++ and Matlab. There are two possible ways:
Call MATLAB Engine directly: Refer to this post for more info. Matlab will run in the background.
Distribute MATLAB into independent shared library: check out here on how to do this (with detail steps and example).

Related

Difference between C++ MEX and C MEX

I wrote a TCPIP-Socket-Connection with Server and Client in C++, which works quite nice in VisualStudio. Now I want to use the C++ - Client in MATLAB/Simulink through MEX-Files and later in a S-Function.
I found two descriptions about MEX-Files.
C++ MEX File Application Just for C++
C/C++ MEX Files C/C++
Now I am confused, which one would be the one to take. I wrote some easy programms with the second, but always got into problems with datatypes. I think, it is because the given examples and functions are only for C and not for C++.
I appreciate any help! Thank you very much!
The differences:
The C interface described in the second link is much, much older (I used this interface way back in 1998). You can create a MEX-file with this interface and have it run on a large set of different versions of MATLAB. You can use it from C as well as C++ code.
The C++-only interface described in the first link is new in MATLAB R2018a (the C++ classes to manipulate MATLAB arrays were introduced in R2017b, but the ability to write a MEX-file was new in R2018a). MEX-files you write with this interface will not run on prior versions of MATLAB.
Additionally, this interface (finally!) allows for creating shared-data copies, in-place operations, etc. (the stuff we have been asking for for many years, but they didn't want to put into the old C interface because they worried it would be too complex for the average MEX-file writer).
Another change to be aware of:
In R2018a, MATLAB also changed the way that complex arrays are stored in memory. In older versions of MATLAB, the real and imaginary components are stored in separate memory blocks. In R2018a and on, they are stored in the same memory block, in the same fashion as you would likely use in your own code.
This affects MEX-files! If you MEX-file uses complex data, it needs to read and write them in the way that MATLAB stores them. If you run a MEX-file compiled for an older version of MATLAB, or compile a MEX-file using the current default building options in R2018a, a complex array will be copied to the old storage model before being passed to the MEX-file. A new compile option to the mex command, -R2018a, creates MEX-files that pass the data in the new storage model unchanged. But those MEX-files will not be compatible with previous versions of MATLAB.
How to choose?
If you need your MEX-files to run on versions of MATLAB prior to the newest R2018a, use the old C interface, you don't have a choice.
If you want to program in C, use the old C interface.
If you need to use complex data, and don't want to incur the cost of the copy, you need to target R2018a and newer, and R2017b and older, separately. You need to write separate code for these two "platforms". The older versions can only be targeted with the C interface. For the newer versions you can use either interface.
If you appreciate the advantages of modern C++ and would like to take advantage of them, and are targeting only the latest and greatest MATLAB version, then use the new C++ interface. I haven't tried it out yet, but from the documentation it looks to be very well designed.

Best way to use Google protocol buffer(protobuf) with Matlab

I am trying to import some protobuf binaries in Matlab. I see 2 ways to do it
1) Use protobuf Matlab plugin
2) Use C++ APIs provided by Google and then import data into matlab using mex files.
Since I am working with large scale data, Which one would be faster to run?
Faster to run is likely to be C++, of those two.
I can tell you definitely that coming up to the Matlab-language level for byte, two-byte, four-byte reads or buffer accesses runs a lot slower than you might expect.
And looking in protobuf-matlab/source/browse/protobuflib, I see tons of that type of operation being done in Matlab code.
You didn't list what I would think to be the best of both worlds: the Java API.
If you have any Java code in your project, or a modest level of comfort with Java, I'd strongly consider using the Java API. Matlab has really good interoperability with Java; it has the big advantage that you can work through examples interactively with methodsview(), etc. There is some learning curve with javaObject()/javaMethod(), vs. when one can use a more native syntax.

Implementing MATLAB function in c++ using Intel C++ compiler

I have developed a MATLAB program with Visual C++. I am using IntelĀ® Integrated Performance Primitives because speed of program is important issue and I have done a lot of efforts for implementing some MATLAB functions. For example, for Min and Max functions over a vector i use ippsMaxIndx_32f; but, there is function in MATLAB like Find.
Here is a description of the Find method in MATLAB:
Description
I need a function which implements this find function of MATLAB with high speed.
Are there any functions inside Intel Ipp, that works like the Find function in MATLAB?
I've never heard of a comprehensive port of matlab functionality to C++. That being said, almost everything matlab does exists within a C/C++ library somewhere, some off the top of my head:
LAPACK, BLAS, and there are a few good implementations, the most notable (free) one being ATLAS.
FFT is implemented in matlab via the fftw library
There are loads of fast open-source image libraries out there, ie. interpolation, filtering.
There are really good OOP matrix libraries out there, boost has a nice one.\
After that, well figure out what you need and there is a good chance someone has implemented it in C/C++.
You can check to these to see if you can find the function you are looking for! As i am not sure for that.
I've used ippsFind_* and they have worked just fine.

(Re)Starting with C++ (for scientific computing)

I have a fair hang of programming in various languages. I have been implementing my codes for research using MATLAB (during the past few months) and for the first time really noticed the difference in execution speed of MATLAB v$ C. (As much as I love the blazingly fast prototyping capabilities).
I am looking to pickup C++ and start using it in my research. I am aware of OOP and have programmed fair bit of Java (relatively long back) and C++ (even longer back). I would like to really get deep into C++ now and hence need suggestions for resources on the same:
What C++ things I need to pick up (STLs and. ) to really make good use of C++?
What is a good tutorial/manual to get started with?
What are the numerical/scientific libraries for C++? GSL? Is there a equivalent (features) of Scipy/Numpy for C++?
I shall be programming on Linux, so I shall be using g++ .
Any pointers to previous SO questions also appreciated.
You'll want to get to grips with parallel programming as quickly as possible. For message-passing I like this book by Karniadakis and Kirby. Of the books on OpenMP, for distributed-memory programming, this one is the best.
If you can get access to them, then Intel's Threading Building Blocks, Maths Kernel Library, and Integrated Performance Primitives are good to have. If not, there are plenty of open source alternatives, start looking at Netlib.
Oh, I almost forgot BOOST, which is a must.
In regards to numerical stuff like Numpy, you should have a look at both:
Blitz++ http://www.oonumerics.org/blitz/
and
Jama/TNT http://math.nist.gov/tnt/download.html
On the library side, check out Armadillo. It almost gives you the full extent of MATLAB's array manipulation syntax and uses LAPACK and BLAS (ATLAS) under the hood.
This tutorial absolutely rocks, but you may not want to tackle it initially.
http://www.parashift.com/c++-faq/
Make sure to read up on the STL (standard template library) and other stuff, using sites like:
http://cplusplus.com/
And, check out the Boost library:
http://www.boost.org/
To make really good use of C++, you need to learn at least the STL, that alone will save you lots of time, but as parashift mentions, C++ OOP is only programming with objects, if you don't use dynamic bindings.
TRNG is a parallel random number generation library. It allows you to create multiple independent streams and was designed for use on clusters.

Plotting data and functions in C++

I am a beginner to programming and I have to generate and plot certain recursions such as x(n+1)=a*x(n)+b*x(n)^2.
I am supposed to use only C/C++ and not mathematical software such as MATLAB, GNU Octave, etc. I am however allowed to use Gnuplot.
Can you tell me what is the best way in which I can arrive at the plots for the above recursions and how I can implement it in my code? Is generating the values, storing it in a file using fstream, and then plotting it later a good idea?
Use the Boost Visualizer Library
http://www.boost.org/doc/libs/1_42_0/libs/graph/doc/index.html
Or go for OGDF using GML output, fairly straightforward
http://www.ogdf.net/ogdf.php?id=
These are all C++ Libraries, not 3rd party software like MATLAB.