Matlab engine - C++ doesn't call matlab function written by myself
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I try to call a matlab function in C++. The matlab function is written by myself. But it seems that C++ doesn't call this function at all. I have successfully run "engwindemo.c". I also do a modification to "engwindemo.c" to make it call a simple function written by myself. It also doesn't work. I am thinking that maybe the function is not put in the right folder. What's the path that I should put my matlab function? Or are there other causes? Thanks for help!
Accepted Answer
James Tursa
on 27 Dec 2013
Edited: James Tursa
on 27 Dec 2013
0 votes
The Engine starts up with the default folder, so if your function is not on the path at start up then the Engine will not see it. You will need to send some "cd" or "path" commands to the Engine first to make the function visible (or maybe have these in a startup.m file that the Engine executes).
5 Comments
Ruiqi Zhao
on 27 Dec 2013
Thank you very much James, it helps. But I still have problem. I write a simple m file, put it in matlab path and call it in "engwindemo.c" and it works this time. But the thing is it still doesn't work for the real m file I want to call both in "engwindemo.c" and my real C++ project. This m file has three inputs and three outputs, one of the inputs in a struct. I add a line "save info.mat" in the m file to examine the input, and it turns out it is not saved. It's still not called. What should I do?
James Tursa
on 27 Dec 2013
Can you post the code for how you are calling this function from your C++ project? And in particular how you are constructing the inputs. Maybe you are using invalid inputs.
Ruiqi Zhao
on 28 Dec 2013
Sure! This is my code. I have been struggling for two days. Thank you for your help!
#include "Prefixspan.h"
#include <engine.h>
#include <matrix.h>
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#pragma comment ( lib, "libmat.lib" )
#pragma comment ( lib, "libmx.lib" )
#pragma comment ( lib, "libmex.lib" )
#pragma comment ( lib, "libeng.lib" )
using namespace std;
#define NUMBER_OF_STRUCTS dbase.size()
#define NUMBER_OF_FIELDS 2
bool Prefixspan::containsearch(Pairdata &projected, ofstream &nodesfile, ofstream &edgefile)
{
if(projected.database.size() < min_sup)
return false;
if(pattern.size() < 2)
{
for (vector<unsigned int>::iterator it = pattern.begin(); it != pattern.end(); it++)
nodesfile << *it << " " << endl;
edgefile << 0 << " " << " " << projected.database.size() << endl;
return true;
}
else
{
*Engine *ep;
mxArray *info, *thresh, *query, *FreCnt, *UniqE, *leg;
ep = engOpen("null");
vector<Transaction> &dbase = projected.database;
vector<Transaction> &elink = projected.edgelink;
double *fn, *fe, *qr;
const char *field_names[2] = {"nodes", "edges"};
mwSize dims[2] = {1, NUMBER_OF_STRUCTS};
info = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names);
for(unsigned int m = 0; m < dbase.size(); m++)
{
mxArray *fieldn, *fielde;
Transaction &transaction = dbase[m];
Transaction &trans = elink[m];
vector<unsigned int> &db = transaction.second;
vector<unsigned int> &el = trans.second;
fieldn = mxCreateDoubleMatrix(1, db.size(), mxREAL);
fn = mxGetPr(fieldn);
for (unsigned int n = 0; n < db.size(); n++)
fn[n] = db[n];
mxSetFieldByNumber(info, m, 0, fieldn);
fielde = mxCreateDoubleMatrix(1, el.size(), mxREAL);
fe = mxGetPr(fielde);
for (unsigned int n = 0; n < el.size(); n++)
fe[n] = el[n];
mxSetFieldByNumber(info, m, 1, fielde);
}
query = mxCreateDoubleMatrix(1, pattern.size(), mxREAL);
qr = mxGetPr(query);
for(unsigned int m = 0; m < pattern.size(); m++)
qr[m] = pattern[m];
thresh = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetPr(thresh) = min_sup;
engPutVariable(ep, "info", info);
engPutVariable(ep, "query", query);
engPutVariable(ep, "thresh", thresh);
engEvalString(ep, "[FreCnt, UniqE, leg] = Search(info, query, thresh)");
FreCnt = engGetVariable(ep, "FreCnt");
UniqE = engGetVariable(ep, "UniqE");
leg = engGetVariable(ep, "leg");
engClose(ep);
mxDestroyArray(info);
mxDestroyArray(query);
mxDestroyArray(thresh);
double *num, *unique, *count;
num = mxGetPr(leg);
unique = mxGetPr(FreCnt);
count = mxGetPr(UniqE);* * **
if(num[0] == 0)
{
mxDestroyArray(FreCnt);
mxDestroyArray(UniqE);
mxDestroyArray(leg);;
return false;
}
else
for (unsigned int m = 0; m < num[0]; m++)
{
for (vector<unsigned int>::iterator it = pattern.begin(); it != pattern.end(); it++)
nodesfile << *it << " ";
nodesfile << endl;
for (unsigned int n = 0; n < pattern.size()*pattern.size(); n++)
edgefile << unique[n * ((unsigned int) num[0]) + m]<<" ";
edgefile << " " << count[m] << endl;
}
mxDestroyArray(FreCnt);
mxDestroyArray(UniqE);
mxDestroyArray(leg);
return true;
}
}
Ruiqi Zhao
on 28 Dec 2013
The function [FreCnt, UniqE, leg] = Search(info, query, thresh) is my matlab function. info is a struct with two fields, query is double vector, thresh is a double number. I checked that the inputs are read well. But still there is no return for the outputs. The matlab function also works well in matlab command. Thanks.
Ruiqi Zhao
on 30 Dec 2013
Hi James, after trial and error, I still couldn't figure out what's wrong with my code, then I gave up matlab engine and used matlab Compiler to create C++ shared libraries instead. It worked perfectly. I only did a small modification on my matlab engine code. So don't bother to examine the code for me any more.
More Answers (0)
Categories
Find more on Simulink Coder in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)