| 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_matlab=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_matlab=(double*)mxMalloc(rows*sizeof(double));
|
|---|
| 79 | for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
|
|---|
| 80 |
|
|---|
| 81 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 82 | mxSetM(dataref,rows);
|
|---|
| 83 | mxSetN(dataref,1);
|
|---|
| 84 | mxSetPr(dataref,vector_matlab);
|
|---|
| 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 | double *tmatrix = NULL;
|
|---|
| 100 |
|
|---|
| 101 | if(matrix){
|
|---|
| 102 | /*create the matlab matrixwith Matlab's memory manager */
|
|---|
| 103 | tmatrix=(double*)mxMalloc(M*N*sizeof(double));
|
|---|
| 104 | for(int i=0;i<M;i++){
|
|---|
| 105 | for(int j=0;j<N;j++){
|
|---|
| 106 | tmatrix[i*N+j]=matrix[j*M+i];
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 110 | mxSetM(dataref,(mwSize)N);
|
|---|
| 111 | mxSetN(dataref,(mwSize)M);
|
|---|
| 112 | mxSetPr(dataref,(double*)tmatrix);
|
|---|
| 113 | }
|
|---|
| 114 | else{
|
|---|
| 115 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 116 | }
|
|---|
| 117 | *pdataref=dataref;
|
|---|
| 118 | }
|
|---|
| 119 | /*}}}*/
|
|---|
| 120 | /*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{1*/
|
|---|
| 121 | void WriteData(mxArray** pdataref,int* matrix, int M,int N){
|
|---|
| 122 |
|
|---|
| 123 | mxArray* dataref = NULL;
|
|---|
| 124 | double* tmatrix = NULL;
|
|---|
| 125 |
|
|---|
| 126 | if(matrix){
|
|---|
| 127 |
|
|---|
| 128 | /*convert to double matrix using Matlab's memory manager*/
|
|---|
| 129 | double* tmatrix=(double*)mxMalloc(M*N*sizeof(double));
|
|---|
| 130 | for(int i=0;i<M;i++){
|
|---|
| 131 | for(int j=0;j<N;j++){
|
|---|
| 132 | tmatrix[i*N+j]=(double)matrix[j*M+i];
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 136 | mxSetM(dataref,(mwSize)N);
|
|---|
| 137 | mxSetN(dataref,(mwSize)M);
|
|---|
| 138 | mxSetPr(dataref,(double*)tmatrix);
|
|---|
| 139 |
|
|---|
| 140 | }
|
|---|
| 141 | else{
|
|---|
| 142 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 143 | }
|
|---|
| 144 | *pdataref=dataref;
|
|---|
| 145 | }
|
|---|
| 146 | /*}}}*/
|
|---|
| 147 | /*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{1*/
|
|---|
| 148 | void WriteData(mxArray** pdataref,double* vector, int M){
|
|---|
| 149 |
|
|---|
| 150 | mxArray* dataref = NULL;
|
|---|
| 151 | double* vector_matlab = NULL;
|
|---|
| 152 |
|
|---|
| 153 | if(vector){
|
|---|
| 154 |
|
|---|
| 155 | /*create the matlab vector with Matlab's memory manager */
|
|---|
| 156 | vector_matlab=(double*)mxMalloc(M*sizeof(double));
|
|---|
| 157 | for(int i=0;i<M;i++) vector_matlab[i]=vector[i];
|
|---|
| 158 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 159 | mxSetM(dataref,(mwSize)M);
|
|---|
| 160 | mxSetN(dataref,(mwSize)1);
|
|---|
| 161 | mxSetPr(dataref,vector_matlab);
|
|---|
| 162 | }
|
|---|
| 163 | else{
|
|---|
| 164 | dataref = mxCreateDoubleMatrix(0,0,mxREAL);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | *pdataref=dataref;
|
|---|
| 168 | }
|
|---|
| 169 | /*}}}*/
|
|---|
| 170 | /*FUNCTION WriteData(mxArray** pdataref,double scalar){{{1*/
|
|---|
| 171 | void WriteData(mxArray** pdataref,double scalar){
|
|---|
| 172 |
|
|---|
| 173 | *pdataref=mxCreateDoubleScalar(scalar);
|
|---|
| 174 | }
|
|---|
| 175 | /*}}}*/
|
|---|
| 176 | /*FUNCTION WriteData(mxArray** pdataref,int integer){{{1*/
|
|---|
| 177 | void WriteData(mxArray** pdataref,int integer){
|
|---|
| 178 |
|
|---|
| 179 | *pdataref=mxCreateDoubleScalar((double)integer);
|
|---|
| 180 |
|
|---|
| 181 | }
|
|---|
| 182 | /*}}}*/
|
|---|
| 183 | /*FUNCTION WriteData(mxArray** pdataref,int boolean){{{1*/
|
|---|
| 184 | void WriteData(mxArray** pdataref,bool boolean){
|
|---|
| 185 |
|
|---|
| 186 | *pdataref=mxCreateDoubleScalar((double)boolean);
|
|---|
| 187 |
|
|---|
| 188 | }
|
|---|
| 189 | /*}}}*/
|
|---|
| 190 | /*FUNCTION WriteData(mxArray** pdataref,char* string){{{1*/
|
|---|
| 191 | void WriteData(mxArray** pdataref,char* string){
|
|---|
| 192 |
|
|---|
| 193 | *pdataref=mxCreateString(string);
|
|---|
| 194 | }
|
|---|
| 195 | /*}}}*/
|
|---|