36 | | Data = mxArray2mat ( mexGetVariable ( "base", set["varname"] ) ); |
37 | | /* UI::get ( rowid, set, "rids" , UI::compulsory ); |
38 | | bdm_assert_debug ( max ( rowid ) <= Data.rows(), "MemDS rowid is too high for given Dat." ); |
39 | | |
40 | | UI::get ( delays, set, "tds", UI::compulsory ); |
41 | | time = max ( delays ); |
42 | | bdm_assert_debug ( time < Data.cols(), "MemDS delays are too high." ); |
43 | | */ |
44 | | //set MemDS |
45 | | dtsize = Data.rows(); |
| 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); |
65 | | /*! |
66 | | * \brief Matlab wrapper for DS mapping functions step() to a matlab function |
67 | | |
68 | | The identifier of matlab function is stored in attribute \c name. |
69 | | This identifier defines: |
70 | | \li function to call to do a step(): name_step.m |
71 | | \li workspace variable to write input to: name_input |
72 | | */ |
73 | | class mexDS : public DS { |
74 | | protected: |
75 | | //! identifier of matlab function |
76 | | string step_name; |
77 | | //! identifier of matlab input variabel |
78 | | string input_name; |
79 | | //! cache of results from name_step |
80 | | vec dt; |
81 | | //! cache of inputs |
82 | | vec ut; |
83 | | public: |
84 | | //!Default constructor |
85 | | mexDS () : DS() {}; |
86 | | |
87 | | /*! \brief Data source computed by matlab function |
88 | | |
89 | | \code |
90 | | system={ |
91 | | class="mexDS"; |
92 | | step_name=""; // name of function to call |
93 | | input_name=""; // name of workspace variable where inputs are written |
94 | | drv = {class='RV',...} // identification of outputs |
95 | | urv = {class='RV',...} // identification of inputs |
96 | | }; |
97 | | \endcode |
98 | | |
99 | | MemDS with the above fields will be created; |
100 | | |
101 | | */ |
102 | | void from_setting ( const Setting &set ) { |
103 | | UI::get ( step_name, set, "step_name", UI::compulsory ); |
104 | | UI::get ( input_name, set, "input_name", UI::compulsory ); |
105 | | |
106 | | shared_ptr<RV> rd = UI::build<RV> ( set, "drv", UI::compulsory ); |
107 | | shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory ); |
108 | | |
109 | | dtsize = rd->_dsize(); |
110 | | utsize = ru->_dsize(); |
111 | | |
112 | | set_drv ( *rd, *ru ); |
113 | | validate(); |
114 | | } |
115 | | |
116 | | virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
117 | | |
118 | | virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
119 | | |
120 | | |
121 | | void step() { |
122 | | mxArray* tmp; |
123 | | mxArray* dummy = NULL; |
124 | | // write inputs to variable input_name |
125 | | mxArray* mxinp = mexGetVariable ( "global", input_name.c_str() ) ; |
126 | | if ( mxinp ) { |
127 | | if ( ( int ) mxGetM ( mxinp ) != utsize || ( int ) mxGetN ( mxinp ) != utsize ) { |
128 | | // mxinp is invalid - create new one |
129 | | mxDestroyArray ( mxinp ); |
130 | | mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL ); |
131 | | } |
132 | | |
133 | | } else { |
134 | | mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL ); |
135 | | } |
136 | | vec2mxArray ( ut, mxinp ); |
137 | | mexPutVariable ( "global", input_name.c_str(), mxinp ); |
138 | | // call function step_name |
139 | | mexCallMATLAB ( 1, &tmp, 0, ( mxArray ** ) &dummy, step_name.c_str() ); |
140 | | // save its results |
141 | | bdm_assert_debug ( ( int ) mxGetM ( tmp ) == dtsize || ( int ) mxGetN ( tmp ) == dtsize, "mexDS.step() expected return vector of length " + num2str ( dtsize ) + |
142 | | "got vector " + num2str ( ( int ) mxGetM ( tmp ) ) + "x" + num2str ( ( int ) mxGetN ( tmp ) ) ); |
143 | | //write y |
144 | | dt.set_subvector ( 0, mxArray2vec ( tmp ) ); |
145 | | //write u |
146 | | } |
147 | | void write ( const vec &ut0 ) { |
148 | | bdm_assert_debug ( ut0.length() == ut.length(), "mexDS: Incompatible input vector" ); |
149 | | ut = ut0; |
150 | | } |
151 | | void getdata ( vec &dt_out ) const { |
152 | | bdm_assert_debug ( dt_out.length() == dt.length(), "mexDS: Incompatible output vector" ); |
153 | | dt_out = dt; |
154 | | } |
155 | | |
156 | | void validate() { |
157 | | dt = zeros ( dtsize ); |
158 | | ut = zeros ( utsize ); |
159 | | } |
160 | | |
161 | | // TODO dodelat void to_setting( Setting &set ) const; |
162 | | }; |
163 | | |
164 | | UIREGISTER ( mexDS ); |
165 | | SHAREDPTR ( mexDS ); |
166 | | |