1 | /* \file WriteData.c:
|
---|
2 | * \brief: general interface for writing data
|
---|
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 | #include "../../include/include.h"
|
---|
12 | #include "../../shared/shared.h"
|
---|
13 | #include "./matlabio.h"
|
---|
14 |
|
---|
15 | #include <mex.h>
|
---|
16 |
|
---|
17 | /*FUNCTION WriteData(mxArray** pdataref,Matrix* matrix){{{1*/
|
---|
18 | void WriteData(mxArray** pdataref,Matrix* matrix){
|
---|
19 |
|
---|
20 | int i,j;
|
---|
21 | mxArray* dataref=NULL;
|
---|
22 | double* matrix_ptr=NULL;
|
---|
23 | int rows,cols;
|
---|
24 | double* tmatrix_ptr=NULL;
|
---|
25 |
|
---|
26 | if(matrix){
|
---|
27 |
|
---|
28 | #ifdef _HAVE_PETSC_
|
---|
29 | PetscMatrixToDoubleMatrix(&matrix_ptr,&rows,&cols,matrix->matrix);
|
---|
30 | #else
|
---|
31 | matrix_ptr=matrix->matrix->ToSerial();
|
---|
32 | matrix->matrix->GetSize(&rows,cols);
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | /*Now transpose the matrix and allocate with Matlab's memory manager: */
|
---|
36 | tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
|
---|
37 | for(i=0;i<cols;i++){
|
---|
38 | for(j=0;j<rows;j++){
|
---|
39 | tmatrix_ptr[i*rows+j]=matrix_ptr[j*cols+i];
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | /*create matlab matrix: */
|
---|
44 | dataref=mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
45 | mxSetM(dataref,rows);
|
---|
46 | mxSetN(dataref,cols);
|
---|
47 | mxSetPr(dataref,tmatrix_ptr);
|
---|
48 |
|
---|
49 | /*Free ressources:*/
|
---|
50 | xfree((void**)&matrix_ptr);
|
---|
51 |
|
---|
52 | }
|
---|
53 | else{
|
---|
54 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
55 | }
|
---|
56 |
|
---|
57 | *pdataref=dataref;
|
---|
58 | }
|
---|
59 | /*}}}*/
|
---|
60 | /*FUNCTION WriteData(mxArray** pdataref,Vector* vector){{{1*/
|
---|
61 | void WriteData(mxArray** pdataref,Vector* vector){
|
---|
62 |
|
---|
63 | mxArray* dataref=NULL;
|
---|
64 | double* vector_ptr=NULL;
|
---|
65 | double* vector_ptr2=NULL;
|
---|
66 | int rows;
|
---|
67 |
|
---|
68 | if(vector){
|
---|
69 | /*call toolkit routine: */
|
---|
70 | #ifdef _HAVE_PETSC_
|
---|
71 | PetscVectorToDoubleVector(&vector_ptr,&rows,vector->vector);
|
---|
72 | #else
|
---|
73 | vector_ptr=vector->vector->ToMPISerial();
|
---|
74 | vector->vector->GetSize(&rows);
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | /*now create the matlab vector with Matlab's memory manager */
|
---|
78 | vector_ptr2=(double*)mxMalloc(rows*sizeof(double));
|
---|
79 | for(int i=0;i<rows;i++) vector_ptr2[i]=vector_ptr[i];
|
---|
80 |
|
---|
81 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
82 | mxSetM(dataref,rows);
|
---|
83 | mxSetN(dataref,1);
|
---|
84 | mxSetPr(dataref,vector_ptr2);
|
---|
85 | }
|
---|
86 | else{
|
---|
87 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*Clean-up and return*/
|
---|
91 | xfree((void**)&vector_ptr);
|
---|
92 | *pdataref=dataref;
|
---|
93 | }
|
---|
94 | /*}}}*/
|
---|
95 | /*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/
|
---|
96 | void WriteData(mxArray** pdataref,double* matrix, int M,int N){
|
---|
97 |
|
---|
98 | mxArray* dataref=NULL;
|
---|
99 | mxArray* tdataref=NULL;
|
---|
100 |
|
---|
101 | if(matrix){
|
---|
102 |
|
---|
103 | /*data is a double* pointer. Copy into a matrix: */
|
---|
104 | tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
105 | mxSetM(tdataref,(mwSize)N);
|
---|
106 | mxSetN(tdataref,(mwSize)M);
|
---|
107 | mxSetPr(tdataref,(double*)matrix);
|
---|
108 |
|
---|
109 | //transpose
|
---|
110 | mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
|
---|
111 | }
|
---|
112 | else{
|
---|
113 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
114 | }
|
---|
115 | *pdataref=dataref;
|
---|
116 | }
|
---|
117 | /*}}}*/
|
---|
118 | /*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{1*/
|
---|
119 | void WriteData(mxArray** pdataref,int* matrix, int M,int N){
|
---|
120 |
|
---|
121 | mxArray* dataref=NULL;
|
---|
122 | mxArray* tdataref=NULL;
|
---|
123 |
|
---|
124 | if(matrix){
|
---|
125 |
|
---|
126 | /*convert to double matrix*/
|
---|
127 | double* doublematrix=(double*)mxMalloc(M*N*sizeof(double));
|
---|
128 | for(int i=0;i<M*N;i++) doublematrix[i]=(double)matrix[i];
|
---|
129 |
|
---|
130 | /*data is a double* pointer. Copy into a matrix: */
|
---|
131 | tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
132 | mxSetM(tdataref,(mwSize)N);
|
---|
133 | mxSetN(tdataref,(mwSize)M);
|
---|
134 | mxSetPr(tdataref,(double*)doublematrix);
|
---|
135 |
|
---|
136 | //transpose
|
---|
137 | mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
|
---|
138 | }
|
---|
139 | else{
|
---|
140 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
141 | }
|
---|
142 | *pdataref=dataref;
|
---|
143 | }
|
---|
144 | /*}}}*/
|
---|
145 | /*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{1*/
|
---|
146 | void WriteData(mxArray** pdataref,double* vector, int M){
|
---|
147 |
|
---|
148 | mxArray* dataref=NULL;
|
---|
149 |
|
---|
150 | if(vector){
|
---|
151 |
|
---|
152 | /*data is a double* pointer. Copy into a vector: */
|
---|
153 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
154 | mxSetM(dataref,(mwSize)M);
|
---|
155 | mxSetN(dataref,(mwSize)1);
|
---|
156 | mxSetPr(dataref,vector);
|
---|
157 | }
|
---|
158 | else{
|
---|
159 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
160 | }
|
---|
161 |
|
---|
162 | *pdataref=dataref;
|
---|
163 | }
|
---|
164 | /*}}}*/
|
---|
165 | /*FUNCTION WriteData(mxArray** pdataref,double scalar){{{1*/
|
---|
166 | void WriteData(mxArray** pdataref,double scalar){
|
---|
167 |
|
---|
168 | *pdataref=mxCreateDoubleScalar(scalar);
|
---|
169 | }
|
---|
170 | /*}}}*/
|
---|
171 | /*FUNCTION WriteData(mxArray** pdataref,int integer){{{1*/
|
---|
172 | void WriteData(mxArray** pdataref,int integer){
|
---|
173 |
|
---|
174 | *pdataref=mxCreateDoubleScalar((double)integer);
|
---|
175 |
|
---|
176 | }
|
---|
177 | /*}}}*/
|
---|
178 | /*FUNCTION WriteData(mxArray** pdataref,int boolean){{{1*/
|
---|
179 | void WriteData(mxArray** pdataref,bool boolean){
|
---|
180 |
|
---|
181 | *pdataref=mxCreateDoubleScalar((double)boolean);
|
---|
182 |
|
---|
183 | }
|
---|
184 | /*}}}*/
|
---|
185 | /*FUNCTION WriteData(mxArray** pdataref,char* string){{{1*/
|
---|
186 | void WriteData(mxArray** pdataref,char* string){
|
---|
187 |
|
---|
188 | *pdataref=mxCreateString(string);
|
---|
189 | }
|
---|
190 | /*}}}*/
|
---|