Main MRPT website > C++ reference for 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
23 {
24 namespace math
25 {
26 #if MRPT_HAS_MATLAB
27 /** \addtogroup matlab_grp Helper functions for MEX & MATLAB
28  * \ingroup mrpt_math_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(
43  mxa); // *IMPORTANT* Matlab stores matrices in *column-major* order!
44  for (size_t j = 0; j < n; j++) // column
45  for (size_t i = 0; i < m; i++) // rows
46  *mxa_data++ = mat.derived().coeff(i, j);
47  return mxa;
48 }
49 
50 /** Convert std::vector<> or std::deque<> of numeric types into Matlab vectors
51  */
52 template <typename CONTAINER>
53 mxArray* convertVectorToMatlab(const CONTAINER& vec)
54 {
55  const size_t m = vec.size(), n = 1;
56  mxArray* mxa = mxCreateDoubleMatrix(m, n, mxREAL);
57  double* mxa_data = mxGetPr(
58  mxa); // *IMPORTANT* Matlab stores matrices in *column-major* order!
59  for (size_t i = 0; i < m; i++) // rows
60  *mxa_data++ = vec[i];
61  return mxa;
62 }
63 
64 /** @} */
65 #endif
66 }
67 }
68 
69 #endif
n
GLenum GLsizei n
Definition: glext.h:5074
Eigen::EigenBase
Definition: eigen_frwds.h:18
mrpt::math::convertToMatlab
mxArray * convertToMatlab(const Eigen::EigenBase< Derived > &mat)
Convert vectors, arrays and matrices into Matlab vectors/matrices.
Definition: utils_matlab.h:38
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::math::convertVectorToMatlab
mxArray * convertVectorToMatlab(const CONTAINER &vec)
Convert std::vector<> or std::deque<> of numeric types into Matlab vectors.
Definition: utils_matlab.h:53
math_frwds.h
mxArray
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.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST