From: Zuochang Ye on
In some senarios the construction of the struct and the function may not be at the same time. For example, if I want to generate a struct for once and later pass the struct to another function along with some data produced by matlab for many times.

This is actually very useful if it can be done nicely. I am also looking forward to such a solution if there is any.

Zuochang
From: Cris Luengo on
The obvious way to do what you want is to make the struct static (for example, http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mexmakememorypersistent.html ). That way you can call the same MEX file multiple times, and have the C/C++ struct available every time you call. The first input argument to your MEX-file would be a command ('open', 'read', 'close'). So you have one MEX-file as an interface to multiple C/C++ functions. On the MATLAB side, you can create multiple M-files that all call the MEX-file with a different command. To make it really pretty you can put the MEX-file inside a PRIVATE/ sub-directory, so that the user can only access it through your M-files.
From: James Tursa on
"Cris Luengo" <cris.luengo(a)google.for.my.name.to.contact.me> wrote in message <hq1l11$3n0$1(a)fred.mathworks.com>...
> The obvious way to do what you want is to make the struct static (for example, http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mexmakememorypersistent.html ). That way you can call the same MEX file multiple times, and have the C/C++ struct available every time you call. The first input argument to your MEX-file would be a command ('open', 'read', 'close'). So you have one MEX-file as an interface to multiple C/C++ functions. On the MATLAB side, you can create multiple M-files that all call the MEX-file with a different command. To make it really pretty you can put the MEX-file inside a PRIVATE/ sub-directory, so that the user can only access it through your M-files.

You will still need to address the issue of whether any dynamically allocated memory is involved.

James Tursa
From: Oliver Woodford on
"Zuochang Ye" wrote:
> In some senarios the construction of the struct and the function may not be at the same time. For example, if I want to generate a struct for once and later pass the struct to another function along with some data produced by matlab for many times.
>
> This is actually very useful if it can be done nicely. I am also looking forward to such a solution if there is any.

I gave an overview of some solutions to a similar problem here very recently:
http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/278243#732195
with an open invitation for better solutions. None have been forthcoming, but you could see if any of the solutions I mention fit your purpose.

Oliver