Changeset 12013 for issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp
- Timestamp:
- 04/16/12 18:37:27 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp
r12011 r12013 9 9 #endif 10 10 11 #include <mex.h> 11 12 #include "../../shared/shared.h" 12 13 #include "../../include/include.h" 13 14 #if defined(_HAVE_MATLAB_) && defined(_SERIAL_) 15 #include <mex.h> 16 /*FUNCTION FetchData(DataSet** pdataset,const mxArray* dataref){{{1*/ 17 void FetchData(DataSet** pdataset,const mxArray* dataref){ 18 19 /*output*/ 20 DataSet* outdataset=NULL; 21 char* outdataset_buffer=NULL; 22 int outdataset_size; 23 24 /*First, check that our reference is a double, otherwise, error out: */ 25 if (mxIsClass(dataref,"double")){ 26 27 /*We need to copy the data pointed by dataref, so that our dataset is not actually a pointer!:*/ 28 if (!mxIsEmpty(dataref)){ 29 outdataset_buffer=(char*)mxGetPr(dataref); 30 outdataset_size=mxGetM(dataref)*mxGetN(dataref); 31 if(outdataset_size)outdataset=DataSetDemarshall(outdataset_buffer); 32 } 33 } 34 else{ 35 if (mxIsEmpty(dataref)){ 36 /*Nothing to pick up. Just initialize pointer to NULL, and warn the server we are not uploading anything: */ 37 outdataset_size=0; 38 outdataset=NULL; 39 } 40 else{ 41 /*This is an error: we don't have the correct input!: */ 42 _error_("Input parameter of class %s not supported yet",mxGetClassName(dataref)); 43 } 44 } 45 46 /*Assign output pointers:*/ 47 *pdataset=outdataset; 48 } 49 /*}}}*/ 14 #include "./matlabio.h" 15 50 16 /*FUNCTION FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref){{{1*/ 51 17 void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref){ … … 560 526 } 561 527 /*}}}*/ 562 /*FUNCTION FetchData(Parameters** pparameters, DataHandle dataref){{{1*/563 void FetchData(Parameters** pparameters, DataHandle dataref){564 565 int i,j;566 int count;567 568 /*output: */569 Param* param=NULL;570 Parameters* parameters=NULL;571 572 /*intermediary: */573 int M,N;574 double* tmatrix=NULL;575 double* matrix=NULL;576 char** stringarray=NULL;577 double** array=NULL;578 int* mdims_array=NULL;579 int* ndims_array=NULL;580 int nfields;581 char* name=NULL;582 mxArray* pfield=NULL;583 mxArray* pfield2=NULL;584 int enum_type;585 586 587 /*First, create parameters : */588 parameters=new Parameters();589 590 /*go through matlab params structure, and create Param object for each field: */591 592 nfields=mxGetNumberOfFields(dataref);593 594 for(count=0;count<nfields;count++){595 596 /*Get i'th field: */597 name=(char*)mxGetFieldNameByNumber(dataref,count);598 enum_type=StringToEnumx(name);599 pfield=mxGetFieldByNumber(dataref,0,count);600 _assert_(pfield);601 602 /*Check type of field: */603 if (mxIsClass(pfield,"double")){604 605 M=mxGetM(pfield);606 N=mxGetN(pfield);607 608 if (M==0 | N==0){609 _error_("array in parameters structure field %s is of size 0",name);610 }611 if (M==1 && N==1){612 /*we have a simple scalar: */613 param= new DoubleParam(enum_type,*mxGetPr(pfield));614 parameters->AddObject(param);615 616 }617 else{618 if (N==1){619 620 /*vector: */621 param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);622 parameters->AddObject(param);623 624 }625 else{626 /*matrix: first, transpose, then plug into Param */627 matrix=mxGetPr(pfield);628 tmatrix=(double*)xmalloc(M*N*sizeof(double));629 for (i=0;i<M;i++){630 for(j=0;j<N;j++){631 *(tmatrix+N*i+j)=*(matrix+M*j+i);632 }633 }634 635 param= new DoubleMatParam(enum_type,tmatrix,M,N);636 parameters->AddObject(param);637 638 /*Free ressources:*/639 xfree((void**)&tmatrix);640 }641 }642 643 }644 else if (mxIsClass(pfield,"logical")){645 646 M=mxGetM(pfield);647 N=mxGetN(pfield);648 649 if (M==0 | N==0){650 _error_("array in parameters structure field %s is of size 0",name);651 }652 if (M==1 && N==1){653 /*we have a simple bool: */654 param= new BoolParam(enum_type,*mxGetLogicals(pfield));655 parameters->AddObject(param);656 657 }658 else{659 _error_("Matrices of Booleans not supported yet in parameters");660 }661 }662 else if (mxIsClass(pfield,"char")){663 /* we have a string parameter:*/664 665 int stringlen;666 char* string=NULL;667 668 stringlen = mxGetM(pfield)*mxGetN(pfield)+1;669 string = (char*)xmalloc(sizeof(mxChar)*stringlen);670 mxGetString(pfield,string,stringlen);671 672 param= new StringParam(enum_type,string);673 parameters->AddObject(param);674 675 xfree((void**)&string);676 }677 else if (mxIsClass(pfield,"cell")){678 679 /*This can be a string array, or a matrix array. Check the first680 *element type to decide: */681 pfield2=mxGetCell(pfield,0);682 if (mxIsClass(pfield2,"char")){683 684 /*string array: */685 M=mxGetM(pfield);686 stringarray=(char**)xmalloc(M*sizeof(char*));687 688 for(i=0;i<M;i++){689 char* descriptor=NULL;690 pfield2=mxGetCell(pfield,i);691 FetchData(&descriptor,pfield2);692 stringarray[i]=descriptor;693 }694 695 param= new StringArrayParam(enum_type,stringarray,M);696 parameters->AddObject(param);697 698 /*Free ressources:*/699 for(i=0;i<M;i++){700 char* descriptor=stringarray[i];701 xfree((void**)&descriptor);702 }703 xfree((void**)&stringarray);704 705 }706 else{707 708 /*matrix array: */709 M=mxGetM(pfield);710 array=(double**)xmalloc(M*sizeof(double*));711 mdims_array=(int*)xmalloc(M*sizeof(int));712 ndims_array=(int*)xmalloc(M*sizeof(int));713 714 for(i=0;i<M;i++){715 double* matrix=NULL;716 int m,n;717 pfield2=mxGetCell(pfield,i);718 FetchData(&matrix,&m,&n,pfield2);719 array[i]=matrix;720 mdims_array[i]=m;721 ndims_array[i]=n;722 }723 724 param= new DoubleMatArrayParam(enum_type,array,M,mdims_array,ndims_array);725 parameters->AddObject(param);726 727 /*Free ressources:*/728 for(i=0;i<M;i++){729 double* matrix=array[i];730 xfree((void**)&matrix);731 }732 xfree((void**)&array);733 xfree((void**)&mdims_array);734 xfree((void**)&ndims_array);735 }736 }737 else _error_("Parameters structure field %s has a format that is not supported: %s",name,mxGetClassName(pfield));738 }739 740 /*Assign output pointers:*/741 *pparameters=parameters;742 }743 /*}}}*/744 #endif
Note:
See TracChangeset
for help on using the changeset viewer.