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 |
|
---|
12 | #ifdef _SERIAL_
|
---|
13 |
|
---|
14 | #include <mex.h>
|
---|
15 |
|
---|
16 | #include "../objects/Param.h"
|
---|
17 | #include "./io.h"
|
---|
18 | #include "../shared/shared.h"
|
---|
19 |
|
---|
20 | #undef __FUNCT__
|
---|
21 | #define __FUNCT__ "FetchParams"
|
---|
22 |
|
---|
23 | void FetchParams(DataSet** pparameters, DataHandle dataref){
|
---|
24 |
|
---|
25 | int i,j,count;
|
---|
26 |
|
---|
27 | /*output: */
|
---|
28 | Param* param=NULL;
|
---|
29 | Numpar* numpar=NULL;
|
---|
30 | DataSet* parameters=NULL;
|
---|
31 |
|
---|
32 | /*intermediary: */
|
---|
33 | int M,N;
|
---|
34 | double* tmatrix=NULL;
|
---|
35 | double* matrix=NULL;
|
---|
36 | char** stringarray=NULL;
|
---|
37 | int nfields;
|
---|
38 | char* name=NULL;
|
---|
39 | mxArray* pfield=NULL;
|
---|
40 | mxArray* pfield2=NULL;
|
---|
41 |
|
---|
42 |
|
---|
43 | /*First, create parameters : */
|
---|
44 | parameters=new DataSet();
|
---|
45 |
|
---|
46 | /*Then, create Numpar object, before building the params objects: */
|
---|
47 | numpar= new Numpar(1);
|
---|
48 | parameters->AddObject(numpar);
|
---|
49 |
|
---|
50 | /*now, go through matlab params structure, and create Param object for each field: */
|
---|
51 |
|
---|
52 | nfields=mxGetNumberOfFields(dataref);
|
---|
53 |
|
---|
54 | for(count=0;count<nfields;count++){
|
---|
55 |
|
---|
56 | /*Get i'th field: */
|
---|
57 | name=(char*)mxGetFieldNameByNumber(dataref,count);
|
---|
58 | pfield=mxGetFieldByNumber(dataref,0,count);
|
---|
59 |
|
---|
60 | /*Check type of field: */
|
---|
61 | if (mxIsDouble(pfield)){
|
---|
62 | /*could be DOUBLE, DOUBLEVEC or DOUBLEMAT, depends on dimensions: */
|
---|
63 |
|
---|
64 | M=mxGetM(pfield);
|
---|
65 | N=mxGetN(pfield);
|
---|
66 |
|
---|
67 | if (M==0 | N==0){
|
---|
68 | throw ErrorException(__FUNCT__,exprintf("%s%s%i%s%i%s%i%s",__FUNCT__," error message: array in parameters structure field ",count," is of size (",M,",",N,")"));
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (M==1 && N==1){
|
---|
72 |
|
---|
73 | /*we have a simple scalar: */
|
---|
74 | param= new Param(count+1,name,DOUBLE);
|
---|
75 | param->SetDouble(*mxGetPr(pfield));
|
---|
76 | parameters->AddObject(param);
|
---|
77 |
|
---|
78 | }
|
---|
79 | else{
|
---|
80 | if (N==1){
|
---|
81 |
|
---|
82 | /*vector: */
|
---|
83 | param= new Param(count+1,name,DOUBLEVEC);
|
---|
84 | param->SetDoubleVec(mxGetPr(pfield),M,1);
|
---|
85 | parameters->AddObject(param);
|
---|
86 |
|
---|
87 | }
|
---|
88 | else{
|
---|
89 |
|
---|
90 | /*matrix: first, transpose, then plug into Param */
|
---|
91 | matrix=mxGetPr(pfield);
|
---|
92 | tmatrix=(double*)xmalloc(M*N*sizeof(double));
|
---|
93 | for (i=0;i<M;i++){
|
---|
94 | for(j=0;j<N;j++){
|
---|
95 | *(tmatrix+N*i+j)=*(matrix+M*j+i);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | param= new Param(count+1,name,DOUBLEMAT);
|
---|
100 | param->SetDoubleMat(tmatrix,M,N);
|
---|
101 | parameters->AddObject(param);
|
---|
102 |
|
---|
103 | /*Free ressources:*/
|
---|
104 | xfree((void**)&tmatrix);
|
---|
105 |
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | }
|
---|
110 | else if (mxIsChar(pfield)){
|
---|
111 |
|
---|
112 | /* string: */
|
---|
113 | param= new Param(count+1,"name",STRING);
|
---|
114 | param->SetString((char*)mxGetChars(pfield));
|
---|
115 | parameters->AddObject(param);
|
---|
116 |
|
---|
117 | }
|
---|
118 | else if (mxIsCell(pfield)){
|
---|
119 |
|
---|
120 | /*string array: */
|
---|
121 | M=mxGetM(pfield);
|
---|
122 | stringarray=(char**)xmalloc(M*sizeof(char*));
|
---|
123 |
|
---|
124 | for(i=0;i<M;i++){
|
---|
125 | char* descriptor=NULL;
|
---|
126 | pfield2=mxGetCell(pfield,i);
|
---|
127 | FetchData(&descriptor,pfield2);
|
---|
128 | stringarray[i]=descriptor;
|
---|
129 | }
|
---|
130 |
|
---|
131 | param= new Param(count+1,name,STRINGARRAY);
|
---|
132 | param->SetStringArray(stringarray,M);
|
---|
133 | parameters->AddObject(param);
|
---|
134 |
|
---|
135 | /*Free ressources:*/
|
---|
136 | for(i=0;i<M;i++){
|
---|
137 | char* descriptor=stringarray[i];
|
---|
138 | xfree((void**)&descriptor);
|
---|
139 | }
|
---|
140 | xfree((void**)&stringarray);
|
---|
141 |
|
---|
142 | }
|
---|
143 | else throw ErrorException(__FUNCT__,exprintf("%s%s%i",__FUNCT__," error message: unknow type in parameters structure field ",i));
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*Assign output pointers:*/
|
---|
147 | *pparameters=parameters;
|
---|
148 |
|
---|
149 | }
|
---|
150 | #endif
|
---|