Passing structures through mex

Ok. I have a structure I want to have access to in my mex file (which I'm writing in C). The values are located in a field within a field (fieldception). Here's an explicit example:
I want to access:
sys.gen.window_size
Where window_size contains an array with the x and y size of a picture (i.e. sys.gen.window_size = size(picture)).
I know you're supposed to use mxGetField. Matlab help says I need to write the field name when sending a structure as an argument, but since the field is a subfield, would I just enter "gen.window_size?"
Or... should I just make life easier and rewrite the structure contain no subfields?
Thanks

 Akzeptierte Antwort

James Tursa
James Tursa am 13 Jan. 2012

2 Stimmen

E.g., the following code snippet w/o any argument checking, assuming you pass in the structure sys as the first argument:
mxArray *gen, *window_size;
double *pr;
:
gen = mxGetField(prhs[0],0,"gen");
window_size = mxGetField(gen,0,"window_size");
pr = mxGetPr(window_size);
pr[0] and pr[1] are your window size values.
NOTE: gen and window_size are pointers to the original fields of your workspace variable. They are not copies, so be sure that you do not call mxDestroyArray on them when you are done.

7 Kommentare

Justin
Justin am 13 Jan. 2012
I will try this out. Thanks for the help!
Justin
Justin am 13 Jan. 2012
Hey James, if window_size is a pointer to the array stored in sys.gen.window_size, can't you just do *window_size to access the first value pointed to by window_size and not have to define the variable pr?
Justin
Justin am 13 Jan. 2012
Epic triple post.
Anyhow, I tried what you suggested and it worked. Thanks! Had minor issues involving the fact that you must declare pr as a double, even though functions such as size in matlab return integers since matlab makes all variables double unless specified. Thanks again.
-Justin
Walter Roberson
Walter Roberson am 13 Jan. 2012
size() returns double precision values that happen to be integral numbers. It is not a matter of how variables are declared, it is a matter of what datatype is returned from size().
James Tursa
James Tursa am 14 Jan. 2012
@Justin: To answer your question, No you cannot do *window_size to access the first value. window_size points to an mxArray variable structure, not to the data within the structure. You have to use mxGetPr (or its cousins) to get at the actual data.
Justin
Justin am 15 Jan. 2012
Thanks again!
Jan
Jan am 15 Jan. 2012
@Justin: Is the problem solved now? Then please accept the answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by