Transfer struct from Matlab to C + +

조회 수: 3 (최근 30일)
Alexmyak
Alexmyak 2012년 11월 8일
답변: alan wang 2015년 5월 26일
I use the library from r2011b stand Matlab to C + +, because there will be where my application will not be installed Matlab. I have a problem with the fact that I need to create a complex subject, and this takes more time, I can not afford to rebuild it every time.
This array is created in Matlab as a struct, I write it to a file "filename.mat" and then open it with matOpen("filename.mat", "r") But the object is empty or not read correctly. One of its fields is an array. When I try to use an object in the library, I get the error:" Attempt to reference field of non-structure array ".
Is there a solution to this problem?
  댓글 수: 2
José-Luis
José-Luis 2012년 11월 8일
Could you post some minimum working example?
Alexmyak
Alexmyak 2012년 11월 8일
편집: Alexmyak 2012년 11월 8일
#include "stdafx.h"
#include "sfam.h"
#include "mclmcrrt.h"
#include "stdio.h"
#include <stdlib.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
mclInitializeApplication(NULL, 0);
sfamInitialize();
// //reading network
MATFile* netfile = matOpen("newstruct.mat", "r");
mxArray* net = matGetVariable(netfile, "tnet");
// //reading data
MATFile* netdatafile = matOpen("minidata.mat", "r");
mxArray* data = matGetVariable(netdatafile, "minidata");
mwArray res;
mwArray debug;
mwArray mwnet = mwArray(net);
mwArray mwdata = mwArray(data);
classify(1,res, mwdata, mwnet ,debug);
matClose(netfile);
matClose(netdatafile);
sfamTerminate();
mclTerminateApplication();
return 0;
}

댓글을 달려면 로그인하십시오.

채택된 답변

Alexmyak
Alexmyak 2012년 11월 13일
I was able to load up the structure, and the data, the code has changed as follows:
#include "stdafx.h"
#include "sfam.h"
#include "mclmcrrt.h"
#include "stdio.h"
#include <stdlib.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
mclInitializeApplication(NULL, 0);
sfamInitialize();
// //reading network
MATFile* netfile = matOpen("D:\\newstruct.mat", "r");
mxArray* net = matGetVariable(netfile, "tnet");
std::cout << "2" << std::endl;
bool isNetStruct = mxIsStruct(net);
std::cout << isNetStruct << std::endl;
// //reading data
MATFile* datafile = matOpen("D:\\data.mat", "r");
mxArray* data = matGetVariable(datafile, "minidata");
// //reading labels
MATFile* labelsfile = matOpen("D:\\labels.mat", "r");
mxArray* labels = matGetVariable(labelsfile, "minilabels");
mxArray *plhs[1];
plhs[0] = NULL;
mxArray *prhs[3];
prhs[0] = data;
prhs[1] = net;
prhs[2] = NULL;
mlxClassify(1, plhs, 3, prhs);
matClose(netfile);
matClose(datafile);
matClose(labelsfile);
sfamTerminate();
mclTerminateApplication();
return 0;
}
Now I call a function, not of mwArray, but from mxArray. But I have a problem in the 34 line. The program just hangs, without issuing any messages.
  댓글 수: 2
José-Luis
José-Luis 2012년 11월 13일
편집: José-Luis 2012년 11월 13일
Which one is line 34? All I can see is that you're passing a NULL pointer (plhs) to mlxClassify. That is, excuse the pun, pointless.
Alexmyak
Alexmyak 2012년 11월 15일
Thank you! I solve  my problem was indeed an error in the fact that I did not initialize starting output parameters (rlhs). This is the last option, the working code can be useful to someone:
#include "stdafx.h"
#include "sfam.h"
#include "mclmcrrt.h"
#include "stdio.h"
#include <stdlib.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
mclInitializeApplication(NULL, 0);
sfamInitialize();
// //reading network
MATFile* netfile = matOpen("D:\\newstruct.mat", "r");
mxArray* net = matGetVariable(netfile, "tnet");
bool isNetStruct = mxIsStruct(net);
// //reading data
MATFile* datafile = matOpen("D:\\data.mat", "r");
mxArray* data = matGetVariable(datafile, "minidata");
// //reading labels
MATFile* labelsfile = matOpen("D:\\labels.mat", "r");
mxArray* labels = matGetVariable(labelsfile, "minilabels");
mxArray* res = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray* debug = mxCreateDoubleMatrix(1, 1, mxREAL);
mxArray* plhs[1];
plhs[0] = res;
mxArray *prhs[3];
prhs[0] = data;
prhs[1] = net;
prhs[2] = debug;
mlxClassify(1, plhs, 3, prhs);
double* dRes = mxGetPr(plhs[0]);
for(int i = 0; i < 3;i++){
std::cout << dRes[i] << std::endl;
}
matClose(netfile);
matClose(datafile);
matClose(labelsfile);
sfamTerminate();
mclTerminateApplication();
return 0;
}

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

alan wang
alan wang 2015년 5월 26일
how's your mlxClassify implemented?

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by