Transfer struct from Matlab to C + +

9 views (last 30 days)
Alexmyak
Alexmyak on 8 Nov 2012
Answered: alan wang on 26 May 2015
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 Comments
José-Luis
José-Luis on 8 Nov 2012
Could you post some minimum working example?
Alexmyak
Alexmyak on 8 Nov 2012
Edited: Alexmyak on 8 Nov 2012
#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;
}

Sign in to comment.

Accepted Answer

Alexmyak
Alexmyak on 13 Nov 2012
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 Comments
José-Luis
José-Luis on 13 Nov 2012
Edited: José-Luis on 13 Nov 2012
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 on 15 Nov 2012
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;
}

Sign in to comment.

More Answers (1)

alan wang
alan wang on 26 May 2015
how's your mlxClassify implemented?

Community Treasure Hunt

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

Start Hunting!