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
2013-12-27
编辑:James Tursa
2013-12-27
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 个评论
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?
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.
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;
}
}
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.
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 Exchange 中查找有关 Simulink Coder 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
