How to create and store a custom structure from C++ into a matlab data file

I wish to generate a struct from c++ to provide the following collection structure in a MATLAB data file.
% Set(1,1).StrA String
% Set(1,1).StrB String
% Set(1,1).StrC String
% Set(1,1).StrD String
% Set(1,1).StrE String
% Set(1,1).Factors Array [ XA, YA, XB, YB, XC, YC, XD, YD, XE, YE ] of double
% Set(1,1).PrimaryMatrix Matrix 256x256 double
% Set(1,1).MaskMatrix Matrix 256x256 int32
% Set(1,1).StatX Matrix 3x256 single
% Set(1,1).StatY Matrix 3x256 single
%
% Set(1,2).StrA String
% Set(1,2).StrB String
% Set(1,2).StrC String
% Set(1,2).StrD String
% Set(1,2).StrE String
% Set(1,2).Factors Array [ XA, YA, XB, YB, XC, YC, XD, YD, XE, YE ] of double
% Set(1,2).PrimaryMatrix Matrix 256x256 double
% Set(1,2).MaskMatrix Matrix 256x256 int32
% Set(1,2).StatX Matrix 3x256 single
% Set(1,2).StatY Matrix 3x256 single
%
% set(1,n)...
The documentation seems pretty clear for standard matrices in a standalone c++ program, and I can create the desired structure in MATLAB, but I am unclear on how to create the nested structure in a standalone c++ program.
Thank you,
Steven.

 Accepted Answer

E.g., a bare bones code outline:
mxArray *mx;
mxArray *Set;
char *fieldnames[] = {"StrA","StrB","StrC","StrD","StrE","Factors","PrimaryMatrix","MaskMatrix","StatX","StatY"};
mwSize n;
n = whatever;
Set = mxCreateStructMatrix(1, n, 10, fieldnames);
mx = mxCreateString("This is (1,1).StrA");
mxSetField(Set, 0, "StrA", mx);
//
mx = mxCreateString("This is (1,1).StrB");
mxSetField(Set, 0, "StrB", mx);
//
mx = mxCreateDoubleMatrix(1,10,mxREAL);
mxSetField(Set, 0, "Factors", mx);
//
mx = mxCreateNumericMatrix(256, 256, mxINT32_CLASS, mxREAL);
mxSetField(Set, 0, "MaskMatrix", mx);
//
mx = mxCreateNumericMatrix(3, 256, mxSINGLE_CLASS, mxREAL);
mxSetField(Set, 0, "StatX", mx);
//
// etc etc etc
For the (1,2) spots use the index 1 instead of 0, etc.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!