Main MRPT website > C++ reference for MRPT 1.5.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-2017, 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
23 {
24  namespace math
25  {
26 #if MRPT_HAS_MATLAB
27  /** \addtogroup matlab_grp Helper functions for MEX & MATLAB
28  * \ingroup mrpt_base_grp
29  * @{ */
30 
31  /** Convert vectors, arrays and matrices into Matlab vectors/matrices.
32  * Supported input classes:
33  * - Eigen::Matrix<T,N,1>
34  * - mrpt::math::CArrayNumeric<T,N>
35  * - mrpt::math::CMatrix{*}
36  */
37  template <typename Derived>
39  {
40  const size_t m = mat.rows(), n = mat.cols();
41  mxArray * mxa = mxCreateDoubleMatrix(m,n,mxREAL);
42  double *mxa_data = mxGetPr(mxa); // *IMPORTANT* Matlab stores matrices in *column-major* order!
43  for (size_t j=0;j<n;j++) // column
44  for (size_t i=0;i<m;i++) // rows
45  *mxa_data++ = mat.derived().coeff(i,j);
46  return mxa;
47  }
48 
49  /** Convert std::vector<> or std::deque<> of numeric types into Matlab vectors */
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(mxa); // *IMPORTANT* Matlab stores matrices in *column-major* order!
56  for (size_t i=0;i<m;i++) // rows
57  *mxa_data++ = vec[i];
58  return mxa;
59  }
60 
61  /** @} */
62 #endif
63  }
64 }
65 
66 #endif
mxArray * convertToMatlab(const Eigen::EigenBase< Derived > &mat)
Convert vectors, arrays and matrices into Matlab vectors/matrices.
Definition: utils_matlab.h:38
GLenum GLsizei n
Definition: glext.h:4618
mxArray * convertVectorToMatlab(const CONTAINER &vec)
Convert std::vector<> or std::deque<> of numeric types into Matlab vectors.
Definition: utils_matlab.h:51
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
struct mxArray_tag mxArray
Forward declaration for mxArray (avoid #including as much as possible to speed up compiling) ...
Definition: CSerializable.h:17



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020