<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - example_mex_callback.cpp</title></head><body bgcolor='white'><pre> <font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt </font> <font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='call_matlab.h.html'>call_matlab.h</a>" <font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='dlib/matrix.h.html'>dlib/matrix.h</a>" <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib; <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std; <font color='#009900'>/* This mex function takes a MATLAB function handle, calls it, and returns the results. For example, you can call this function in MATLAB like so: A = magic(3) y = example_mex_callback(A, @(x)x+x) This will result in y containing the value 2*A. */</font> <font color='#0000FF'><u>void</u></font> <b><a name='mex_function'></a>mex_function</b> <font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> matrix<font color='#5555FF'><</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>></font><font color='#5555FF'>&</font> A, <font color='#0000FF'>const</font> function_handle<font color='#5555FF'>&</font> f, matrix<font color='#5555FF'><</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>></font><font color='#5555FF'>&</font> result <font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// The f argument to this function is a function handle passed from MATLAB. To </font> <font color='#009900'>// call it we use the following syntax: </font> <font color='#BB00BB'>call_matlab</font><font face='Lucida Console'>(</font>f, A, <font color='#BB00BB'>returns</font><font face='Lucida Console'>(</font>result<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; <font color='#009900'>// This is equivalent to result = f(A). Therefore, the returns(variable) syntax </font> <font color='#009900'>// is used to indicate which variables are outputs of the function. </font> <font color='#009900'>// Another thing we can do is call MATLAB functions based on their string name </font> <font color='#009900'>// rather than a function_handle. Here is an example of calling eigs(). </font> matrix<font color='#5555FF'><</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>></font> <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#979000'>2</font>,<font color='#979000'>2</font><font face='Lucida Console'>)</font>; m <font color='#5555FF'>=</font> <font color='#979000'>1</font>,<font color='#979000'>2</font>, <font color='#979000'>3</font>,<font color='#979000'>4</font>; matrix<font color='#5555FF'><</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>></font> v,d; <font color='#009900'>// This is equivalent to [v,d] = eigs(m); </font> <font color='#BB00BB'>call_matlab</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>eigs</font>", m, <font color='#BB00BB'>returns</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>, <font color='#BB00BB'>returns</font><font face='Lucida Console'>(</font>d<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>; cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>eigenvectors: \n</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> v <font color='#5555FF'><</font><font color='#5555FF'><</font> endl; cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>eigenvalues: \n</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> d <font color='#5555FF'><</font><font color='#5555FF'><</font> endl; <b>}</b> <font color='#009900'>// #including this brings in all the mex boiler plate needed by MATLAB. </font><font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='mex_wrapper.cpp.html'>mex_wrapper.cpp</a>" </pre></body></html>