root/library/bdm/mex/mex_datasource.h @ 1059

Revision 1058, 1.7 kB (checked in by smidl, 14 years ago)

doc + DS cleanup

  • Property svn:eol-style set to native
Line 
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
9namespace bdm {
10/*!
11* \brief DataSource reading data columns from a Matlab matrix
12
13The data are stored in a arbitrary Matlab matrix. Each column of the matrix corresponds to one discrete time observation \f$t\f$.
14The DataSource needs to know the name of the matrix and possibly decription of its contents (via RV)_
15
16*/
17class 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;
25public:
26       
27        //!Default constructor
28        mxArrayDS () : DS() {};
29
30        /*! \brief Create data source from mxArray
31
32        \code
33        class   = 'mxArrayDS';
34        varname = 'data_matrix';         // name of a workspace variable
35        drv     = {class='RV',...}       // names of data in columns
36        \endcode
37       
38        */
39        void from_setting ( const Setting &set ) {
40                string name;
41                UI::get(name, set, "varname", UI::compulsory);
42                mxArray *mxAr = mexGetVariable ( "base", name.c_str() );
43               
44                bdm_assert(mxIsNumeric(mxAr),"Matlab variable: "+name +" is not a matrix");
45               
46                dtsize = mxGetM(mxAr);
47                max_len= mxGetN(mxAr);
48                data = (double*)mxGetPr(mxAr);
49                utsize = 0;
50
51                DS::from_setting(set); //read drv
52               
53                if (Drv._dsize()!=dtsize){
54                        Drv=RV( ( const char* ) set["varname"], dtsize );
55                }
56                column = 0;
57       
58                if (max_length()>0){
59                        dt = vec(&data[column*dtsize],dtsize);
60                }
61        }
62
63        int max_length() {return max_len;}
64       
65        void step(){
66                if (column<max_length()){
67                        column++;
68               
69                        dt = vec(&data[column*dtsize],dtsize);
70                } else {
71                        bdm_error("DS: trying to read after max_length()");
72                }
73        }
74};
75
76UIREGISTER ( mxArrayDS );
77SHAREDPTR ( mxArrayDS );
78
79}
80#endif //MXDS_H
Note: See TracBrowser for help on using the browser.