7

I have a C++ program in Visual Studio that records data and saves it into a file. I want to do some Matlab analysis reading from that file and save the results in a separate one. Then, my C++ program keeps going.

Is there any way to do this automatically coding the call in C++ when Matlab is open in the same computer?

Thanks in advance!

Slash
  • 285
  • 2
  • 13
  • 1
    There seems to be a couple of useful links [here](https://social.msdn.microsoft.com/Forums/en-US/cdce8399-95f9-4a67-a2dd-0b57bf9bf24c/calling-matlab-from-c?forum=vclanguage) – Thomas Russell Aug 20 '15 at 10:30
  • 1
    There appear to be some duplicates, see [here](http://stackoverflow.com/questions/23061113/how-to-execute-a-matlab-function-in-ms-visual-c), [here](http://stackoverflow.com/questions/6429243/how-to-call-matlab-functions-from-c) [and](http://stackoverflow.com/questions/26774830/call-matlab-in-c-code-using-methods-in-engine-h) [many](http://stackoverflow.com/questions/1576876/matlab-in-c-c-and-c-c-in-matlab) [many](http://stackoverflow.com/questions/7757613/calling-matlab-from-c) [more](http://stackoverflow.com/questions/9654965/how-do-i-use-matlab-engine-in-my-code-for-calling-engopensingleuse) – hbaderts Aug 20 '15 at 10:52
  • Thanks! I read through them, and it is a possible solution that I had found, but that is to translate a Matlab code to C++. However, I am using a whole Matlab Toolbox, that is why my need is to call Matlab from VS and execute the script in Matlab rather thatn translate the toolbox, which I can't do. – Slash Aug 20 '15 at 10:57

3 Answers3

8

There are many ways to call MATLAB from C++ depending on your needs. Many similar questions have been asked here in the past and I will refer to those and as well give you a solution as your requirement seems to be different.

  1. Do you want MATLAB C or C++ API? Then use mex functions described here, here, here and the actual MATLAB documentation
  2. Do you want to convert your MATLAB program to C++? Then use MATLAB Coder described here, here and here
  3. Do you want to run a MATLAB script from within C++? Then call MATLAB Engine or write a shell script and have that called from C++ described here and here

Your problem falls under the third category. So you need to either call MATLAB engine (See Tal Darom's answer) or write a shell script. I will explain the latter. Lets write a shell script called matlab_script.sh:

#/bin/sh
matlab -nodisplay -nojvm -nosplash < your_matlab_file.m 

then in your C++ code do this:

system("matlab_script.sh");

You need matlab_script.sh to be executable. Under linux you normally do chmod +x matlab_script.sh

Community
  • 1
  • 1
romeric
  • 2,325
  • 3
  • 19
  • 35
  • You don't need to go over a shell script, and a user of a program using the Matlab Engine does not need to have Matlab installed to run it, by using the Matlab Compiler. – rubenvb Aug 20 '15 at 12:07
  • Isn't Matlab compiler SDK too much of an overkill for OP's needs? He is using Matlab toolboxes where he probably wants to interactively, edit both his Matlab scripts and his C++ code. His Matlab and C++ programs actually never to talk to each other. C++ writes and Matlab reads, basic IO. And a shell script will do exactly that. – romeric Aug 20 '15 at 13:23
  • It is not an answer to current OP's question, but important note: MATLAB Engine needs to have MATLAB installed at the same PC, while using .exe and .dll and etc sometimes needs just MATLAB Runtime. – Mikhail_Sam Aug 11 '17 at 11:50
  • Does the batch script solution running matlab on the command line also work on a Windows machine? – tzg Aug 26 '22 at 20:27
  • Update: Here's a working solution on Windows 10: >matlab -nodesktop -nosplash " your_matlab_file; exit;" – tzg Aug 29 '22 at 13:41
1

You can start a matlab engine from within a program, and run matlab scripts using matlab engine API.

see documentation at: http://www.mathworks.com/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html

Tal Darom
  • 1,379
  • 1
  • 8
  • 26
1

You can make use of the Matlab Compiler SDK, which turns your matlab code into a standalone program or library.

You can then call this libary from your C++ code, and the end user won't even need to have Matlab installed (just the Compiler Runtime, which you can deploy "royalty-free"), see this web page for what you can do with it.

rubenvb
  • 74,642
  • 33
  • 187
  • 332