1 | #ifndef MXDS_H |
---|
2 | #define MXDS_H |
---|
3 | |
---|
4 | |
---|
5 | #include "../bdm/bdmerror.h" |
---|
6 | #include "../bdm/base/datasources.h" |
---|
7 | #include "mex_parser.h" |
---|
8 | |
---|
9 | namespace bdm { |
---|
10 | /*! |
---|
11 | * \brief DataSource reading data columns from a Matlab matrix |
---|
12 | |
---|
13 | The data are stored in a arbitrary Matlab matrix. Each column of the matrix corresponds to one discrete time observation \f$t\f$. |
---|
14 | The DataSource needs to know the name of the matrix and possibly decription of its contents (via RV)_ |
---|
15 | |
---|
16 | */ |
---|
17 | class mxArrayDS : public DS { |
---|
18 | protected: |
---|
19 | //! raw pointer to data |
---|
20 | double *data; |
---|
21 | //! maximum length of data |
---|
22 | int max_len; |
---|
23 | //! active column |
---|
24 | int column; |
---|
25 | public: |
---|
26 | |
---|
27 | //!Default constructor |
---|
28 | mxArrayDS () : DS() {}; |
---|
29 | |
---|
30 | /*! Create object from the following structure |
---|
31 | |
---|
32 | \code |
---|
33 | class = 'mxArrayDS'; |
---|
34 | varname = 'data_matrix'; % name of a Matlab workspace variable |
---|
35 | -- inherited fields --- |
---|
36 | bdm::DS::from_setting |
---|
37 | drv = RV({'names',...},[sizes,...]); % names of data redords in columns |
---|
38 | \endcode |
---|
39 | |
---|
40 | */ |
---|
41 | void from_setting ( const Setting &set ) { |
---|
42 | string name; |
---|
43 | UI::get(name, set, "varname", UI::compulsory); |
---|
44 | mxArray *mxAr = mexGetVariable ( "base", name.c_str() ); |
---|
45 | |
---|
46 | bdm_assert(mxIsNumeric(mxAr),"Matlab variable: "+name +" is not a matrix"); |
---|
47 | |
---|
48 | dtsize = mxGetM(mxAr); |
---|
49 | max_len= mxGetN(mxAr); |
---|
50 | data = (double*)mxGetPr(mxAr); |
---|
51 | utsize = 0; |
---|
52 | |
---|
53 | DS::from_setting(set); //read drv |
---|
54 | |
---|
55 | if (Drv._dsize()!=dtsize) { |
---|
56 | Drv=RV( ( const char* ) set["varname"], dtsize ); |
---|
57 | } |
---|
58 | column = 0; |
---|
59 | |
---|
60 | if (max_length()>0) { |
---|
61 | dt = vec(&data[column*dtsize],dtsize); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | int max_length() { |
---|
66 | return max_len; |
---|
67 | } |
---|
68 | |
---|
69 | void step() { |
---|
70 | if (column<max_length()) { |
---|
71 | column++; |
---|
72 | |
---|
73 | dt = vec(&data[column*dtsize],dtsize); |
---|
74 | } else { |
---|
75 | bdm_error("DS: trying to read after max_length()"); |
---|
76 | } |
---|
77 | } |
---|
78 | }; |
---|
79 | |
---|
80 | UIREGISTER ( mxArrayDS ); |
---|
81 | SHAREDPTR ( mxArrayDS ); |
---|
82 | |
---|
83 | } |
---|
84 | #endif //MXDS_H |
---|