From: meisterbartsch on
Hi NG,

I want to load a structure from a matlab file (*.mat) into vc++. In
the best case I would have a matching structure in vc++ to access the
data of the loaded matlab structure.

Model.torque.ratio=[1,0.5,0.2,0.1,0], in Matlab

may be

vector<double> Model.torque.ratio;

in VC++;

Does anyone of you have an idea how to get this working?

Best regards

Patrick

From: John Reilly on
Hi Patrick,

I did the same thing in C# (2005), and it will be even easier in C++.

You want to look at the documentation:

<http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f39876.html#f13830>

which is MATLAB -> External Interfaces -> Importing and
Exporting Data -> Using MAT files -> Reading and Writing MAT
files

TMW provides a nice C API for manipulating MAT files. Works great.
Just remember that matrices are column-major in MAT files and
row-major in C/C++/C# and you are golden.

Somewhere in the help docs you can find the MAT file format, but I
prefer to rely on their library, which I know works, rather than
debug my own.

good luck.

john.

meisterbartsch wrote:
>
>
> Hi NG,
>
> I want to load a structure from a matlab file (*.mat) into vc++. In
> the best case I would have a matching structure in vc++ to access
> the
> data of the loaded matlab structure.
>
> Model.torque.ratio=[1,0.5,0.2,0.1,0], in Matlab
>
> may be
>
> vector<double> Model.torque.ratio;
>
> in VC++;
>
> Does anyone of you have an idea how to get this working?
>
> Best regards
>
> Patrick
>
>
From: meisterbartsch on
On 13 Jun., 13:46, "John Reilly" <jrhokie1@_NOSPAM_yahoo.com> wrote:
> Hi Patrick,
>
> I did the same thing in C# (2005), and it will be even easier in C++.
>
> You want to look at the documentation:
>
> <http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external...>
>
> which is MATLAB -> External Interfaces -> Importing and
> Exporting Data -> Using MAT files -> Reading and Writing MAT
> files
>
> TMW provides a nice C API for manipulating MAT files. Works great.
> Just remember that matrices are column-major in MAT files and
> row-major in C/C++/C# and you are golden.
>
> Somewhere in the help docs you can find the MAT file format, but I
> prefer to rely on their library, which I know works, rather than
> debug my own.
>
> good luck.
>
> john.
>
> meisterbartsch wrote:
>
> > Hi NG,
>
> > I want to load a structure from a matlab file (*.mat) into vc++. In
> > the best case I would have a matching structure in vc++ to access
> > the
> > data of the loaded matlab structure.
>
> > Model.torque.ratio=[1,0.5,0.2,0.1,0], in Matlab
>
> > may be
>
> > vector<double> Model.torque.ratio;
>
> > in VC++;
>
> > Does anyone of you have an idea how to get this working?
>
> > Best regards
>
> > Patrick

Hi, and thanks for your quick answer I got some code for you which is
working so far but I do not have any clue how to the data out of
"Model.torque.ratio" ? Do you have any Idea?
pa only gives me a pointer to mxArray but I do not know how to deal
with this array now.


Code follows:

#include "mat.h"

CString file="test.mat";
MATFile *pmat;
const char **dir;
const char *name;
int ndir;
int i;
mxArray *pa;

pmat=matOpen(file,"r");
if (pmat == NULL) {
TRACE("\"%s\" could not be opened!\n",file);
}
else
{
/*
* get directory of MAT-file
*/
dir = (const char **)matGetDir(pmat, &ndir);
if (dir == NULL) {
TRACE("Error reading directory of file %s\n", file);
} else {
TRACE("Directory of %s:\n", file);
for (i=0; i < ndir; i++)
TRACE("%s\n",dir[i]);
}
pa=matGetVariable(pmat, "Model");
mxFree(dir);
}

From: John Reilly on
Hi Patrick,

now comes the fun part; seriously, i like this stuff. you now need
to use the MX array API to get to the data:

<http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/bqoqnz0.html>

you'll use mxGetField to get a the field "torque" from 'pa'. then
you'll use that result in mxGetField again to get the field "ratio".
then use mxGetPr to get a pointer to the data. you can use
mxGetDimensions if you don't know the dimensions of "ratio" a priori.

don't forget that the data is column-major if "ratio" is a
multi-dimensional matrix.

good luck.

john.
From: Patrick Bartsch on
Ah OK I see...

thank you very much... I am going to try this tomorrow...

John Reilly schrieb:
> Hi Patrick,
>
> now comes the fun part; seriously, i like this stuff. you now need
> to use the MX array API to get to the data:
>
> <http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/bqoqnz0.html>
>
> you'll use mxGetField to get a the field "torque" from 'pa'. then
> you'll use that result in mxGetField again to get the field "ratio".
> then use mxGetPr to get a pointer to the data. you can use
> mxGetDimensions if you don't know the dimensions of "ratio" a priori.
>
> don't forget that the data is column-major if "ratio" is a
> multi-dimensional matrix.
>
> good luck.
>
> john.