Matlab engine - C++ doesn't call matlab function written by myself

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!

 採用された回答

James Tursa
James Tursa 2013 年 12 月 27 日
編集済み: James Tursa 2013 年 12 月 27 日

0 投票

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 件のコメント

Ruiqi Zhao
Ruiqi Zhao 2013 年 12 月 27 日
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
James Tursa 2013 年 12 月 27 日
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
Ruiqi Zhao 2013 年 12 月 28 日
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
Ruiqi Zhao 2013 年 12 月 28 日
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
Ruiqi Zhao 2013 年 12 月 30 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink Coder についてさらに検索

タグ

質問済み:

2013 年 12 月 27 日

コメント済み:

2013 年 12 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by