matlab - How to use a struct in a mex file

It took me some time until I figured out how to pass a struct to a mex file and especially how to get out the values.

To get the values from the struct following lines are important:

 /* get the values from the struct */
  const mxArray  *mxTmp; 
  double         *tmp;
 
  mxTmp = mxGetField(prhs[1],0,"par1");
  tmp   = mxGetPr(mxTmp); 

Here the full code

 

To compile the code, put the code in a file, e.g. passStruct2Mex_MEX.c and use following command within matlab

cd /path/to/file
mex passStruct2Mex_MEX.c

 

To test the code you can use following matlab script

% test the mex function

approxPar.par1 = [1,2,3,4];
approxPar.par2 = [10,20,30,40];
approxPar.par3 = [100,200,300,400];
approxPar.delta = 0.02;

distMtx = rand(375,375,375);
triangulared_distMtx = zeros(375,375,375);
passStruct2Mex_MEX(distMtx,approxPar,triangulared_distMtx);
keyboard

It should print out the following lines

Is Structure or Not: 1
1st field name: par1
2ns field name: par2
3rd field name: par3
4th field name: delta
5th field name: (null)
 
 ----- printing values of par1 -----
par1[0] = 1.00
par1[1] = 2.00
par1[2] = 3.00
par1[3] = 4.00
 

 

blog_tags: 

Comments

Hi there are a few execution errors while running the program such as syntax errors and undeclared identifiers.Hope you could give me a elaborate explaination in executing this file

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <img> <br> <p> <span>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Code snippets in <code>...</code> or <source>...</source> automatically will be pretty printed.
  • Use [collapse] and [/collapse] to create collapsible text blocks. [collapse collapsed] or [collapsed] will start with the block closed.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image