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 Center および File ExchangeC Shared Library Integration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by