Ejemplo n.º 1
0
void EchoIt(MLINK stdlink,MLINK ml,bool b) {
  bool inCompound = false;
  int i;
  double d;
  char * s = 0;
  char * t = 0;
  long m,n;
  switch(MLGetType(stdlink)) {
  case MLTKINT;
     MLGetInteger(stdlink,&i);
     MLPutInteger(ml,i);
     break;
  case MLTKSYMB;
     MLGetSymbol(stdlink,&s);
     t = new char[strlen(s)+4];
     strcpy(t,s);
     strcat(t,"MXS");
     MLPutSymbol(ml,t);
     delete [] t;
     MLDisownSymbol(stdlink,s);
     break;
  case MLTKSTR;
     MLGetString(stdlink,&s);
     MLPutString(ml,s);
     MLDisownString(stdlink,s);
     break;
  case MLTKINT;
     MLGetInteger(stdlink,&i);
     MLPutInteger(ml,i);
     break;
  case MLTKFUNC;
     MLGetFunction(stdlink,&s,m);
     strcpy(t,s);
     strcat(t,"MXS");
     if(strcmp(s,"CompoundExpression")==0 && b) {
       inCompound = true;
       MLPutFunction(ml,t,2*m);
     } else {
       MLPutFunction(ml,t,m);
     };
     delete [] t;
     for(long n=1;n<=m;++n) {
       if(inCompound) {
         MLPutFunction(ml,"Print",4L); 
         MLPutString(ml,"Function:");
         MLPutString(ml,s);
         MLPutString(ml," ");
         MLPutInteger(ml,s_number);
         ++s_number;
       };
       EchoIt(stdlink,ml,b);
     };
     inCompound = false;
     break;
  default:
     DBG();
     break;
  };
};
Ejemplo n.º 2
0
void eng_make_Struct() {
    const char *name;
    int field_count;
    MLGetFunction(stdlink, &name, &field_count);

    std::vector<const char *> field_names(field_count);
    for (int i=0; i < field_count; ++i) {
        MLGetString(stdlink, &(field_names[i]));
    }

    int *handle_list;
    int handle_count;
    MLGetInteger32List(stdlink, &handle_list, &handle_count);

    int *mmDims;
    int depth;
    MLGetInteger32List(stdlink, &mmDims, &depth);

    std::vector<mwSize> mbDimsVec(depth);
    std::reverse_copy(mmDims, mmDims+depth, mbDimsVec.begin());
    mwSize *mbDims = &mbDimsVec[0];

    mxArray *var = mxCreateStructArray(depth, mbDims, field_count, &(field_names[0]));

    int len = handle_count / field_count;
    assert(mxGetNumberOfElements(var) == len);
    for (int i=0; i < len; ++i)
        for (int j=0; j < field_count; ++j) {
            mxSetFieldByNumber(var, i, j, handles.value(handle_list[i*len + j]));
            handles.remove(handle_list[i*len + j]); // remove to avoid double mxFree()
        }

    for (int i=0; i < field_count; ++i)
        MLReleaseString(stdlink, field_names[i]);

    MLReleaseInteger32List(stdlink, handle_list, handle_count);
    MLReleaseInteger32List(stdlink, mmDims, depth);

    returnHandle(var);
}