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

Revision 1060, 1.8 kB (checked in by smidl, 14 years ago)

doc

  • 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        /*! 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() {return max_len;}
66       
67        void step(){
68                if (column<max_length()){
69                        column++;
70               
71                        dt = vec(&data[column*dtsize],dtsize);
72                } else {
73                        bdm_error("DS: trying to read after max_length()");
74                }
75        }
76};
77
78UIREGISTER ( mxArrayDS );
79SHAREDPTR ( mxArrayDS );
80
81}
82#endif //MXDS_H
Note: See TracBrowser for help on using the browser.