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



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020