Calling matlab from c++

Hello! I created a simply matlab funcion
function addFunc(a,b)
plot(a,b);
end
and I compiled it to a lib and dll.
#include "stdafx.h"
#include "engine.h"
#include "addFunc.h"
#pragma comment(lib, "libmat.lib")
#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libmex.lib")
#pragma comment(lib, "libeng.lib")
#pragma comment(lib, "addFunc.lib")
#pragma comment(lib, "mclmcrrt.lib")
So what next? how can I call this function from VS?
Thanks!

7 Commenti

James Tursa
James Tursa il 16 Mag 2018
It's not clear what you are trying to do. Why is your code compiled into a lib and dll? What other code is using this lib or dll? Is that other code starting a MATLAB Engine? Are you trying to plot native C++ variables using the MATLAB Engine? Etc etc.
We need more details about what specifically you are trying to do.
Yoni Berk
Yoni Berk il 17 Mag 2018
Ok so I am trying to learn how to connect matlab code to c++ code for stand alone app. So if I have a c++ program that do all the calculations and I have an algorithm in Matlab or a function that do plot visualation (like the function that I wrote), how can I call this function / algorithm from c++?
James Tursa
James Tursa il 17 Mag 2018
By "standalone" do you mean running on a machine without MATLAB installed? Or do you mean a program that starts a MATLAB Engine off to the side and sends variables there for processing/plotting?
Yoni Berk
Yoni Berk il 17 Mag 2018
Modificato: Yoni Berk il 17 Mag 2018
For a machine without MATLAB installed
Yoni Berk
Yoni Berk il 17 Mag 2018
Modificato: Yoni Berk il 17 Mag 2018
Ok I changed the matlab code, it is now just doing a meshgrid of 2 vectors.
function [X,Y] = meshgridC(vecX,vecY)
[X,Y] = meshgrid(vecX,vecY);
end
again I compiled it and linked it to VS. (maybe I missed there something)
and this is my c++ code that calls the matlab functions:
#include "stdafx.h"
#include "meshgridC.h"
#pragma comment(lib, "libmat.lib")
#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libmex.lib")
#pragma comment(lib, "libeng.lib")
#pragma comment(lib, "meshgridC.lib")
#pragma comment(lib, "mclmcrrt.lib")
int main()
{
const int size = 100;
double x[size] = { 0 };
double y[size] = { 0 };
for (int i = 1; i <= 99; i++)
{
x[i] = x[i - 1] + 0.01;
y[i] = x[i] * x[i];
}
// Initialize the MATLAB Compiler Runtime global state
mclInitializeApplication(NULL, 0)
if (!mclInitializeApplication(NULL, 0))
{
std::cerr << "Could not initialize the application properly."
<< std::endl;
return -1;
}
// Initialize the Vigenere library
meshgridCInitialize();
if (!meshgridCInitialize())
{
std::cerr << "Could not initialize the library properly."
<< std::endl;
return -1;
}
mwArray rX, rY;
mwArray vecX, vecY;
vecX = x;
vecY = y;
meshgridC(2, rX, rY, vecX, vecY);
meshgridCTerminate();
mclTerminateApplication();
}
when I run the program, the consul application is writing me an error:
An error has occurred while trying to initialize the MATLAB Runtime.
The error is: Fatal error loading library c:\users\...\VSProject\X64\Debug\libmat.dll
Error: The specified module could not be found.
Could not initialize the application properly.
How can I slove that?
Update: I copied the dlls to the project folder and it is "working". But I got initialization error from this section:
// Initialize the MATLAB Compiler Runtime global state
mclInitializeApplication(NULL, 0)
if (!mclInitializeApplication(NULL, 0))
{
std::cerr << "Could not initialize the application properly."
<< std::endl;
return -1;
}
Yoni Berk
Yoni Berk il 17 Mag 2018
Sove this part. I removed the mclInitializeApplication(NULL, 0) line before the if statement. Now I have an error at the library initialization part. It writes me that function or variable 'javaaddpath' is undefined. :\

Accedi per commentare.

Risposte (1)

Akshat Dalal
Akshat Dalal il 12 Mag 2025

0 voti

Hi Yoni,
My understanding is that the error you are facing regarding "that function or variable 'javaaddpath' is undefined" is expected. This is because the generated code probably has some interfacing with MATLAB which is being used to execute MATLAB code on the MATLAB engine. However, since you're trying to create a standalone application with MATLAB, 'javaaddpath' being an inbuild MATLAB function is undefined. You can debug this further to identify where the MATLAB Engine is being exectued and instead use your own custom piece of code. I might be wrong but this is what seems to be the issue.

Categorie

Prodotti

Release

R2017b

Richiesto:

il 16 Mag 2018

Risposto:

il 12 Mag 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by