Converting Matlab to C++ - c++

I need to convert some MATLAB code to C++. I am using Visual Studio 2010 and have MATLAB compiler installed. I am wondering how do I go about doing this.
Also when I publish my project will the end user have to install anything by MATLAB? (this cannot be the case)

You can compile Matlab scripts into standalone executables, but I'm not familiar with a Matlab tool to convert Matlab code to Visual C++ code. You can convert Matlab code to C or C++ using Simulink Coder (formerly Real-Time Workshop), which is available from Mathworks as a separate product.
To run the standalone executable, the user will have to install the Matlab Compiler Runtime. From the Matlab Compiler web page:
Executables and libraries created with the MATLAB Compiler product use a runtime engine called the MATLAB Compiler Runtime (MCR). The MCR is provided with MATLAB Compiler for distribution with your application and can be deployed royalty-free.

You will not get rid of the Matlab runtimes. I often had problems using Matlab Compiler. It basically is like a wrapper around the Matlab runtime, generates clumsy code and did introduce many problems for deployment. C alternatives ... ? I could not find any. So I recently switched to ilnumerics, which is like Matlab in C# - only much faster and no translation necessary. it works out great so far because we use ASP.NET anyway and hence save jumping between languages and can directly use the 3d plots of ilnumerics.

Another option you have is to use the MATLAB Engine. It probably isn't an option for real-time applications, but otherwise it's really nice. You basically call MATLAB functions right from C++. Figures (plots) will be display just as if you were running local MATLAB. When the program starts, a MATLAB console is created.

Related

Any way to profile C++ MEX code NOT built with the 'Mex' command?

I need to profile some C++ Mex code.
It is built with G++, using arguments similar to those mex would pass. I can absolutely pass -o0 and -g or -g3, no problem.
The current solutions I have found:
MATLAB Profiler - Requires it was built with mex. I tried this myself to no avail. (It provided no information)
Visual Studio - Also apparently requires that it was built with mex. I tried this myself with these results sadly. (It just froze)
MATLAB Engine - Apparently also requires it was built with mex.
Building a C++ driver for it instead of the MATLAB script is likely not an option, as the script is complex and does significant setup (some with other mex files) before the section I want is called.
Is there anything that will work with MEX files not built with mex?

How to deploy a Tensorflow trained model for inference for a Windows standalone application

I would like to use a model trained with Tensorflow in a Windows standalone desktop application. I only need to perform predictions, I can train the model with Tensorflow Python API. What is the recommended approach?
I know there is a C++ API, but it is really hard to compile it, especially on Windows. Can I find any prebuilt C++ Tensorflow binaries for Windows?
Is there an easy way to distribute Python with Tensorflow as a Windows installer prerequisite?
Can I import the Tensorflow model in another technology and use it for inference? OpenCv DNN module has a function which imports data from Tensorflow, but I understood it has many limitations, and I was not able to import and use a model with OpenCv.
Thanks for help!
I was challenging the same issues as you.
You should at least try to compile it (try CMake, it might be easier)
If you still having trouble:
Compiler is out of Heap Space
Standalone Windows Lib
Basic Tensorflow Handling with C++
I asked a similar question and eventually found my own way to the answer. In the end, I found the Tensorflow instructions were actually pretty good (it was my reading them that was bad!). I have not tried using Bazel for Windows, but building Tensorflow using CMake ended up working fine.
The main issue was the compiler heap space issue. This always seems to occur in some random place if you are using the MS Visual Studio 32-bit compiler (default). The key is to make sure you run vcvarsall.bat or vcvars64.bat or whatever it takes to invoke the 64-bit compiler (in Task Manager, it should show up as cl.exe, not cl.exe *32) I found it hard (read: impossible) to get Visual Studio to use the 64-bit compiler, but using the MSBuild tool to compile on the command line worked fine.
Once you can build the example program, you have an example of an application that links to a static tensorflow library to do its stuff. You can just make your own application link to this library for what you want.

How do I run an Answer Set Programming file from a C++ File?

What methods are there that I can use to run an ASP file from the main function of my C++ code?
I'm doing a project in which I am expected to control a simulated turtlebot using a C++ file, and use ASP code to construct an action plan that the robot can use to achieve a specified goal.
I'm using Ubuntu 14.04, SPARC (an ASP solver), ROS indigo, and gazebo.
Am I correct that SPARC is implemented in Java (this link)?
If this is the case, there are quite a few options:
You can use Java Native Interface to interact with SPARC tool directly from c++ code
Other option is to create a separate
process
from c++ code and communicate with it via stdout/files
You can implement ROS node in Java which accepts requests and uses SPARC as a library to process them. In this case your c++ doesn't care about exact SPARC implementation
Resume:
I would suggest you to try the last option. It's slightly more difficult than other 2 options, but it fits perfectly into ROS infrastructure.

Integrate MATLAB code in Visual Studio 2010 (C++)

I've seen that it is possible to call MATLAB functions from VS2010 (VC++). I was wondering if it is possible to create executable files using VC++ that do not require any MATLAB software on the computer (like the MATLAB Compiler Runtime (MCR))?
I would like to create a program written in VC++ that calls some of the MATLAB functions, but is completely standalone and can run from any Windows computer without the need of any prior MATLAB software to have been installed.
Thanks.
It is not possible to install without MCR in any documented way and I even think that it is not legal.
However, there are some examples like Imatest, who managed to do it in some of their earlier versions.
Edit(1)
I almost forgot about MatlabCoder. If you buy it, it can transform your Matlab code directly into c code. But it will not work with some of the toolboxes, like image processing.

OpenCV, Matlab and STL containers

Many functions in the latest release of OpenCV require the use of STL containers. I run into problems when trying to use them in a Matlab MEX file. I am compiling the MEX files from within Matlab. Both OpenCV and Matlab use the "/MD" flag which is "Multithreaded DLL" for code generation.
Compiler: MSVC++ 9.0
Matlab 2010a
OpenCV latest from SVN, 2.11 I think.
The code I am using is very simple:
vector<KeyPoint> keypoints_vec;
SurfFeatureDetector surf;
surf.detect(cvImg,keypoints_vec);
This compiles but crashes when run in a Matlab MEX file. The crash is within OpenCV in vector::resize. The old interface (without STL containers) works fine but is deprecated. How can I use STL containers between Matlab and OpenCV?
I fought with this very problem in the last two days. The problem is this:
libmex.dll (and a whole Matlab) uses Microsoft.VC80.CRT (version=8.0.50727.4053)
But your OpenCV uses Microsoft.VC90.CRT (version=9.0.21022.8)
So you can use the previous version of VC (VS 2005 with SP1 as far as I know), or as a workaround, you can use gcc (MINGW) (in this case they use totally different stl, so they can't interfere).
I did the latter and it works, and it will work with the next versions of Matlab (hopefully).
A long time ago I had problems with Matlab <-> VS interop. It might be some microsoft visual c++ runtime library discrepancy. Check what runtime lib is required by matlab and what version does your visual studio have. I remember using Depends to get the dll dependencies for my program. Check your call stack after crashing (by attaching your msdev debugger) it might give you some hints.
It was a long time ago so I'm just giving hints of what I remember.
I had a similar problem in the past few days, and was able to resolve the issue with some help from the friendly folks at MathWorks.
From the original post at http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error :
You are probably seeing an incompatibility between the stl library and or compiler options used by your pre-compiled dll and those used by MATLAB and the MEX command. MATLAB 2009b was built with MSVC 2005.
You may be able to fix the problem by changing the options used by mex or by building your mex file directly with MSVC. One example of an option that may effect things is SECURE_SCL=0. I would start by building your test program with the options MATLAB is using to find the problematic option then try removing that option when building the mex file.
Because of this sort of incompatibility use of stl objects in the api's of third party compiled libraries is usually a bad idea.
Following his advice, I removed the SECURE_SCL=0 option from the mex options file at
C:\Users\ThePit\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat
Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.
The data in a vector should still be stored as a single contiguous block
std::vector<int> data;
int *array = &data[0];
int *array = &data.front();
Should give you 'c' style pointers to the data, try passing these to matlab
see also: How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?