1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
---|
3 | <title>mixpp: How to write and use mex files for Matlab</title> |
---|
4 | <link href="tabs.css" rel="stylesheet" type="text/css"> |
---|
5 | <link href="doxygen.css" rel="stylesheet" type="text/css"> |
---|
6 | </head><body> |
---|
7 | <!-- Generated by Doxygen 1.5.8 --> |
---|
8 | <script type="text/javascript"> |
---|
9 | <!-- |
---|
10 | function changeDisplayState (e){ |
---|
11 | var num=this.id.replace(/[^[0-9]/g,''); |
---|
12 | var button=this.firstChild; |
---|
13 | var sectionDiv=document.getElementById('dynsection'+num); |
---|
14 | if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){ |
---|
15 | sectionDiv.style.display='block'; |
---|
16 | button.src='open.gif'; |
---|
17 | }else{ |
---|
18 | sectionDiv.style.display='none'; |
---|
19 | button.src='closed.gif'; |
---|
20 | } |
---|
21 | } |
---|
22 | function initDynSections(){ |
---|
23 | var divs=document.getElementsByTagName('div'); |
---|
24 | var sectionCounter=1; |
---|
25 | for(var i=0;i<divs.length-1;i++){ |
---|
26 | if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){ |
---|
27 | var header=divs[i]; |
---|
28 | var section=divs[i+1]; |
---|
29 | var button=header.firstChild; |
---|
30 | if (button!='IMG'){ |
---|
31 | divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild); |
---|
32 | button=document.createElement('img'); |
---|
33 | divs[i].insertBefore(button,divs[i].firstChild); |
---|
34 | } |
---|
35 | header.style.cursor='pointer'; |
---|
36 | header.onclick=changeDisplayState; |
---|
37 | header.id='dynheader'+sectionCounter; |
---|
38 | button.src='closed.gif'; |
---|
39 | section.id='dynsection'+sectionCounter; |
---|
40 | section.style.display='none'; |
---|
41 | section.style.marginLeft='14px'; |
---|
42 | sectionCounter++; |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | window.onload = initDynSections; |
---|
47 | --> |
---|
48 | </script> |
---|
49 | <div class="navigation" id="top"> |
---|
50 | <div class="tabs"> |
---|
51 | <ul> |
---|
52 | <li><a href="main.html"><span>Main Page</span></a></li> |
---|
53 | <li class="current"><a href="pages.html"><span>Related Pages</span></a></li> |
---|
54 | <li><a href="modules.html"><span>Modules</span></a></li> |
---|
55 | <li><a href="annotated.html"><span>Classes</span></a></li> |
---|
56 | <li><a href="files.html"><span>Files</span></a></li> |
---|
57 | </ul> |
---|
58 | </div> |
---|
59 | <div class="navpath"><a class="el" href="manual.html">User Manual</a> |
---|
60 | </div> |
---|
61 | </div> |
---|
62 | <div class="contents"> |
---|
63 | <h1><a class="anchor" name="mexfiles">How to write and use mex files for Matlab </a></h1><h2><a class="anchor" name="use"> |
---|
64 | Howto use predefined mexfiles</a></h2> |
---|
65 | A range of mexfiles is predefined in directory <code>library/mex</code>. Many of these mexfile process ui files (see <a class="el" href="ui.html">User Infos and their use</a>) examples of these files are in directory <code>library/tutorial</code>. Note that in order to run these files you need to let matlab know where to find them: <div class="fragment"><pre class="fragment">>> addpath path_to_bdm/library/mex |
---|
66 | </pre></div><p> |
---|
67 | Then, you can go to <code>library/tutorial</code> and run e.g. <code>arx_test_mex</code>.<h2><a class="anchor" name="write"> |
---|
68 | Howto write custom mex file</a></h2> |
---|
69 | Due to special nature of the mex files, the mex file can be split in three parts: <ul> |
---|
70 | <li>input conversion, where input arguments are converted to IT++ structures, </li> |
---|
71 | <li>main body of algorithm, where any C++ bdm constructions can be used, </li> |
---|
72 | <li>output conversion, where resulting IT++ structures are converted to mxArrays.</li> |
---|
73 | </ul> |
---|
74 | The first and the third part is achieved using prepared IT++ routines, see IT++ documentation.<p> |
---|
75 | Script <code></code>./buildmex is prepared to compile and link the mexfile with bdm. <div class="fragment"><pre class="fragment">$ ./buildmex.sh my_mex_file.cpp |
---|
76 | </pre></div> on Linux, or <div class="fragment"><pre class="fragment">$ ./buildmex.bat my_mex_file.cpp |
---|
77 | </pre></div> on Windows.<p> |
---|
78 | Example of a mexfile: <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <itpp/itmex.h></span> |
---|
79 | <span class="preprocessor">#include <<a class="code" href="arx_8h.html" title="Bayesian Filtering for generalized autoregressive (ARX) model.">estim/arx.h</a>></span> |
---|
80 | |
---|
81 | <span class="keyword">using namespace </span>bdm; |
---|
82 | |
---|
83 | <span class="keywordtype">void</span> mexFunction(<span class="keywordtype">int</span> n_output, mxArray *output[], <span class="keywordtype">int</span> n_input, <span class="keyword">const</span> mxArray *input[]) |
---|
84 | { |
---|
85 | <span class="comment">// Check the number of inputs and output arguments</span> |
---|
86 | <span class="keywordflow">if</span>(n_output!=1) mexErrMsgTxt(<span class="stringliteral">"Wrong number of output variables!"</span>); |
---|
87 | <span class="keywordflow">if</span>(n_input!=2) mexErrMsgTxt(<span class="stringliteral">"Usage: arx1d(ysize, Data)!"</span>); |
---|
88 | |
---|
89 | <span class="comment">// Convert input variables to IT++ format</span> |
---|
90 | <span class="keywordtype">int</span> ysize = mxArray2int(input[0]); |
---|
91 | mat Data = mxArray2mat(input[1]); |
---|
92 | |
---|
93 | <span class="comment">// ------------------ Start of routine ---------------------------</span> |
---|
94 | ARX Ar; |
---|
95 | Ar.set_statistics(ysize, 1e-5*eye(Data.rows()) ); |
---|
96 | Ar.bayesB(Data); |
---|
97 | <span class="comment">// ------------------ End of routine -----------------------------</span> |
---|
98 | |
---|
99 | <span class="comment">// Create output vectors</span> |
---|
100 | output[0] = mxCreateDoubleMatrix(1,Data.rows(), mxREAL); |
---|
101 | |
---|
102 | <span class="comment">// Convert the IT++ format to Matlab format for output</span> |
---|
103 | vec2mxArray(Ar.posterior().mean(), output[0]); |
---|
104 | } |
---|
105 | </pre></div> </div> |
---|
106 | <hr size="1"><address style="text-align: right;"><small>Generated on Thu Apr 23 21:06:43 2009 for mixpp by |
---|
107 | <a href="http://www.doxygen.org/index.html"> |
---|
108 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address> |
---|
109 | </body> |
---|
110 | </html> |
---|