Show
Ignore:
Timestamp:
10/29/09 23:23:56 (15 years ago)
Author:
smidl
Message:

mex tutorial cleanup + working mexDS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/mex/mex_datasource.h

    r676 r685  
    9191        \code 
    9292        system={ 
    93                 type="mexDS"; 
     93                class="mexDS"; 
    9494                step_name="";              // name of function to call 
    9595                input_name="";             // name of workspace variable where inputs are written 
    96                 rv_out = {class='RV',...}  // identification of outputs 
    97                 rv_in = {class='RV',...}   // identification of inputs 
     96                yrv = {class='RV',...}  // identification of outputs 
     97                urv = {class='RV',...}   // identification of inputs 
    9898                }; 
    9999        \endcode 
     
    106106                UI::get(input_name, set, "input_name", UI::compulsory); 
    107107                 
    108                 shared_ptr<RV> ry = UI::build<RV> ( set, "rv_out", UI::compulsory ); 
    109                 shared_ptr<RV> ru = UI::build<RV> ( set, "rv_in", UI::compulsory); 
     108                shared_ptr<RV> ry = UI::build<RV> ( set, "yrv", UI::compulsory ); 
     109                shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory); 
    110110 
    111                 dtsize=ry->_dsize(); 
     111                ytsize=ry->_dsize(); 
    112112                utsize=ru->_dsize(); 
    113  
     113                dtsize = ytsize+utsize; 
     114                 
    114115                set_drv(*ry, *ru); 
     116                validate(); 
    115117        } 
    116118         
    117119        void step() { 
    118120                mxArray* tmp; 
    119                 mxArray* tmp2; 
     121                mxArray* dummy=NULL; 
    120122                // write inputs to variable input_name 
    121123                mxArray* mxinp= mexGetVariable ( "global", input_name.c_str()) ; 
     124                if (mxinp){ 
     125                        if((int)mxGetM(mxinp)!=utsize || (int)mxGetN(mxinp)!=utsize) { 
     126                                // mxinp is invalid - create new one 
     127                                mxDestroyArray(mxinp); 
     128                                mxinp=mxCreateDoubleMatrix(utsize,1,mxREAL); 
     129                        } 
     130                         
     131                } else { 
     132                        mxinp=mxCreateDoubleMatrix(utsize,1,mxREAL); 
     133                } 
    122134                vec2mxArray(ut, mxinp); 
     135                mexPutVariable("global",input_name.c_str(),mxinp); 
    123136                // call function step_name 
    124                 mexCallMATLAB(1, &tmp, 0, (mxArray **) &tmp2, step_name.c_str()); 
     137                mexCallMATLAB(1, &tmp, 0, (mxArray **) &dummy, step_name.c_str()); 
    125138                // save its results 
    126                 dt=mxArray2vec(tmp); 
     139                bdm_assert_debug((int)mxGetM(tmp)==ytsize || (int)mxGetN(tmp)==ytsize,"mexDS.step() expected return vector of length " + num2str(dtsize) + 
     140                "got vector " + num2str((int)mxGetM(tmp)) + "x" + num2str((int)mxGetN(tmp))); 
     141                //write  y 
     142                dt.set_subvector(0,mxArray2vec(tmp)); 
     143                //write u 
     144                dt.set_subvector(ytsize,ut); 
    127145        } 
    128         void write(const vec &ut0){ ut=ut0;} 
    129         void getdata(vec &dt_out) const {dt_out = dt;   } 
     146        void write(const vec &ut0){  
     147                bdm_assert_debug(ut0.length()==ut.length(),"mexDS: Incompatible input vector"); 
     148                ut=ut0; 
     149        } 
     150        void getdata(vec &dt_out) const { 
     151                bdm_assert_debug(dt_out.length()==dt.length(),"mexDS: Incompatible output vector"); 
     152                dt_out = dt;     
     153        } 
    130154 
     155        void validate() { 
     156                dt=zeros(dtsize); 
     157                ut=zeros(utsize); 
     158        } 
    131159 
    132160        // TODO dodelat void to_setting( Setting &set ) const;