MRPT  1.9.9
utils_matlab.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef mrpt_math_utils_matlab_H
10 #define mrpt_math_utils_matlab_H
11 
12 /** \file Provide helper functions for MEX/MATLAB.
13  * This file can be safely included without checking MRPT_HAS_MATLAB
14  */
15 
16 #include <mrpt/config.h>
17 #if MRPT_HAS_MATLAB
18 #include <mexplus.h>
19 #endif
20 #include <mrpt/math/math_frwds.h>
21 
22 namespace mrpt::math
23 {
24 #if MRPT_HAS_MATLAB
25 /** \addtogroup matlab_grp Helper functions for MEX & MATLAB
26  * \ingroup mrpt_math_grp
27  * @{ */
28 
29 /** Convert vectors, arrays and matrices into Matlab vectors/matrices.
30  * Supported input classes:
31  * - Eigen::Matrix<T,N,1>
32  * - mrpt::math::CArrayNumeric<T,N>
33  * - mrpt::math::CMatrix{*}
34  */
35 template <typename Derived>
37 {
38  const size_t m = mat.rows(), n = mat.cols();
39  mxArray* mxa = mxCreateDoubleMatrix(m, n, mxREAL);
40  double* mxa_data = mxGetPr(
41  mxa); // *IMPORTANT* Matlab stores matrices in *column-major* order!
42  for (size_t j = 0; j < n; j++) // column
43  for (size_t i = 0; i < m; i++) // rows
44  *mxa_data++ = mat.derived().coeff(i, j);
45  return mxa;
46 }
47 
48 /** Convert std::vector<> or std::deque<> of numeric types into Matlab vectors
49  */
50 template <typename CONTAINER>
51 mxArray* convertVectorToMatlab(const CONTAINER& vec)
52 {
53  const size_t m = vec.size(), n = 1;
54  mxArray* mxa = mxCreateDoubleMatrix(m, n, mxREAL);
55  double* mxa_data = mxGetPr(
56  mxa); // *IMPORTANT* Matlab stores matrices in *column-major* order!
57  for (size_t i = 0; i < m; i++) // rows
58  *mxa_data++ = vec[i];
59  return mxa;
60 }
61 
62 /** @} */
63 #endif
64 }
65 #endif
66 
67 
mxArray * convertToMatlab(const Eigen::EigenBase< Derived > &mat)
Convert vectors, arrays and matrices into Matlab vectors/matrices.
Definition: utils_matlab.h:36
GLenum GLsizei n
Definition: glext.h:5074
This base provides a set of functions for maths stuff.
mxArray * convertVectorToMatlab(const CONTAINER &vec)
Convert std::vector<> or std::deque<> of numeric types into Matlab vectors.
Definition: utils_matlab.h:51
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling) ...
Definition: CSerializable.h:18



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020