acado_mat_file.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
34 
36 
37 template<typename T>
38 void MatFile< T >::write( std::ostream& stream,
39  const GenericMatrix< T >& mat,
40  const char* name
41  )
42 {
43  Fmatrix x;
44  double tmp;
45 
46  x.type = 0000; // IEEE Little Endian - reserved - double precision (64 bits) - numeric full matrix
47  x.mrows = mat.getNumRows(); // number of rows
48  x.ncols = mat.getNumCols(); // number of columns
49  x.imagf = 0; // no imaginary part
50  x.namelen = 1+strlen(name); // matrix name length
51 
52  stream.write( (char*) &x, sizeof(Fmatrix));
53  stream.write(name, x.namelen);
54 
55  // mat files store data in column-major format
56  for (uint col = 0; col < mat.getNumCols(); ++col)
57  {
58  for (uint row = 0; row < mat.getNumRows(); ++row)
59  {
60  tmp = mat(row, col);
61  stream.write((char*) &tmp, sizeof(double));
62  }
63  }
64 
65  // imaginary numbers should be stored just after the real ones
66 }
67 
68 template<typename T>
69 void MatFile< T >::write( std::ostream& stream,
70  const GenericVector< T >& vec,
71  const char* name
72  )
73 {
74  Fmatrix x;
75  double tmp;
76 
77  x.type = 0000; // IEEE Big Endian - reserved - double precision (64 bits) - numeric full matrix
78  x.mrows = vec.getDim(); // number of rows
79  x.ncols = 1; // number of columns
80  x.imagf = 0; // no imaginary part
81  x.namelen = 1+strlen(name); // matrix name length
82 
83  stream.write( (char*) &x, sizeof(Fmatrix));
84  stream.write(name, x.namelen);
85 
86  // mat files store data in column-major format
87  for (uint row=0; row<vec.getDim(); ++row)
88  {
89  tmp = vec( row );
90  stream.write((char*) &tmp, sizeof(double));
91  }
92 
93  // imaginary numbers should be stored just after the real ones
94 }
95 
96 //
97 // Explicit instantiations of templates.
98 //
99 template class MatFile< double >;
100 template class MatFile< int >;
101 template class MatFile< bool >;
102 
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
Simple class for writing binary data file that are compatible with Matlab.
#define CLOSE_NAMESPACE_ACADO
unsigned getDim() const
Definition: vector.hpp:172
unsigned getNumRows() const
Definition: matrix.hpp:185
RowXpr row(Index i)
Definition: BlockMethods.h:725
unsigned getNumCols() const
Definition: matrix.hpp:189
#define BEGIN_NAMESPACE_ACADO
void write(std::ostream &stream, const GenericMatrix< T > &mat, const char *name)
ColXpr col(Index i)
Definition: BlockMethods.h:708


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:27