[2333] | 1 | /* \file FetchParams.c:
|
---|
| 2 | * \brief: interface for fetching matlab parameters into a "C" dataset
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #ifdef HAVE_CONFIG_H
|
---|
| 6 | #include "config.h"
|
---|
| 7 | #else
|
---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | #ifdef _SERIAL_
|
---|
| 12 |
|
---|
| 13 | #include <mex.h>
|
---|
| 14 | #include "./io.h"
|
---|
[3685] | 15 | #include "../objects/objects.h"
|
---|
[2333] | 16 | #include "../shared/shared.h"
|
---|
[3332] | 17 | #include "../include/macros.h"
|
---|
[3685] | 18 | #include "../EnumDefinitions/EnumDefinitions.h"
|
---|
[2333] | 19 |
|
---|
[3713] | 20 | void FetchParams(Parameters** pparameters, DataHandle dataref){
|
---|
[2333] | 21 |
|
---|
[3673] | 22 | int i,j;
|
---|
[3685] | 23 | int count;
|
---|
[2333] | 24 |
|
---|
| 25 | /*output: */
|
---|
| 26 | Param* param=NULL;
|
---|
[3673] | 27 | Parameters* parameters=NULL;
|
---|
[2333] | 28 |
|
---|
| 29 | /*intermediary: */
|
---|
| 30 | int M,N;
|
---|
| 31 | double* tmatrix=NULL;
|
---|
| 32 | double* matrix=NULL;
|
---|
| 33 | char** stringarray=NULL;
|
---|
| 34 | int nfields;
|
---|
| 35 | char* name=NULL;
|
---|
| 36 | mxArray* pfield=NULL;
|
---|
| 37 | mxArray* pfield2=NULL;
|
---|
[3673] | 38 | int enum_type;
|
---|
[2333] | 39 |
|
---|
| 40 |
|
---|
| 41 | /*First, create parameters : */
|
---|
[3673] | 42 | parameters=new Parameters();
|
---|
[2333] | 43 |
|
---|
[3673] | 44 | /*go through matlab params structure, and create Param object for each field: */
|
---|
[2333] | 45 |
|
---|
| 46 | nfields=mxGetNumberOfFields(dataref);
|
---|
| 47 |
|
---|
| 48 | for(count=0;count<nfields;count++){
|
---|
| 49 |
|
---|
| 50 | /*Get i'th field: */
|
---|
| 51 | name=(char*)mxGetFieldNameByNumber(dataref,count);
|
---|
[3673] | 52 | enum_type=StringAsEnum(name);
|
---|
[2333] | 53 | pfield=mxGetFieldByNumber(dataref,0,count);
|
---|
| 54 |
|
---|
| 55 | /*Check type of field: */
|
---|
| 56 | if (mxIsDouble(pfield)){
|
---|
| 57 | /*could be DOUBLE, DOUBLEVEC or DOUBLEMAT, depends on dimensions: */
|
---|
| 58 |
|
---|
| 59 | M=mxGetM(pfield);
|
---|
| 60 | N=mxGetN(pfield);
|
---|
| 61 |
|
---|
| 62 | if (M==0 | N==0){
|
---|
[3570] | 63 | ISSMERROR("%s%i (%s) %s%i%s%i%s","array in parameters structure field ",count,name," is of size (",M,",",N,")");
|
---|
[2333] | 64 | }
|
---|
| 65 | if (M==1 && N==1){
|
---|
| 66 | /*we have a simple scalar: */
|
---|
[3673] | 67 | param= new DoubleParam(enum_type,*mxGetPr(pfield));
|
---|
[2333] | 68 | parameters->AddObject(param);
|
---|
| 69 |
|
---|
| 70 | }
|
---|
| 71 | else{
|
---|
| 72 | if (N==1){
|
---|
| 73 |
|
---|
| 74 | /*vector: */
|
---|
[3673] | 75 | param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
|
---|
[2333] | 76 | parameters->AddObject(param);
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 | else{
|
---|
| 80 | /*matrix: first, transpose, then plug into Param */
|
---|
| 81 | matrix=mxGetPr(pfield);
|
---|
| 82 | tmatrix=(double*)xmalloc(M*N*sizeof(double));
|
---|
| 83 | for (i=0;i<M;i++){
|
---|
| 84 | for(j=0;j<N;j++){
|
---|
| 85 | *(tmatrix+N*i+j)=*(matrix+M*j+i);
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[3673] | 89 | param= new DoubleMatParam(enum_type,tmatrix,M,N);
|
---|
[2333] | 90 | parameters->AddObject(param);
|
---|
| 91 |
|
---|
| 92 | /*Free ressources:*/
|
---|
| 93 | xfree((void**)&tmatrix);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | }
|
---|
| 98 | else if (mxIsChar(pfield)){
|
---|
| 99 |
|
---|
[2337] | 100 | /* we have a string parameter:*/
|
---|
| 101 |
|
---|
| 102 | int stringlen;
|
---|
| 103 | char* string=NULL;
|
---|
| 104 |
|
---|
| 105 | stringlen = mxGetM(pfield)*mxGetN(pfield)+1;
|
---|
| 106 | string = (char*)xmalloc(sizeof(mxChar)*stringlen);
|
---|
| 107 | mxGetString(pfield,string,stringlen);
|
---|
| 108 |
|
---|
[3673] | 109 | param= new StringParam(enum_type,string);
|
---|
[2333] | 110 | parameters->AddObject(param);
|
---|
| 111 |
|
---|
[2337] | 112 | xfree((void**)&string);
|
---|
[2333] | 113 | }
|
---|
| 114 | else if (mxIsCell(pfield)){
|
---|
| 115 |
|
---|
| 116 | /*string array: */
|
---|
| 117 | M=mxGetM(pfield);
|
---|
| 118 | stringarray=(char**)xmalloc(M*sizeof(char*));
|
---|
| 119 |
|
---|
| 120 | for(i=0;i<M;i++){
|
---|
| 121 | char* descriptor=NULL;
|
---|
| 122 | pfield2=mxGetCell(pfield,i);
|
---|
| 123 | FetchData(&descriptor,pfield2);
|
---|
| 124 | stringarray[i]=descriptor;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[3673] | 127 | param= new StringArrayParam(enum_type,stringarray,M);
|
---|
[2333] | 128 | parameters->AddObject(param);
|
---|
| 129 |
|
---|
| 130 | /*Free ressources:*/
|
---|
| 131 | for(i=0;i<M;i++){
|
---|
| 132 | char* descriptor=stringarray[i];
|
---|
| 133 | xfree((void**)&descriptor);
|
---|
| 134 | }
|
---|
| 135 | xfree((void**)&stringarray);
|
---|
| 136 |
|
---|
| 137 | }
|
---|
[3570] | 138 | else ISSMERROR("%s%i","unknow type in parameters structure field ",i);
|
---|
[2333] | 139 | }
|
---|
| 140 |
|
---|
| 141 | /*Assign output pointers:*/
|
---|
| 142 | *pparameters=parameters;
|
---|
| 143 |
|
---|
| 144 | }
|
---|
| 145 | #endif
|
---|