Changeset 12040
- Timestamp:
- 04/17/12 20:14:35 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp
r12037 r12040 63 63 mxArray* dataref=NULL; 64 64 double* vector_ptr=NULL; 65 double* vector_ ptr2=NULL;65 double* vector_matlab=NULL; 66 66 int rows; 67 67 … … 76 76 77 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];78 vector_matlab=(double*)mxMalloc(rows*sizeof(double)); 79 for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i]; 80 80 81 81 dataref = mxCreateDoubleMatrix(0,0,mxREAL); 82 82 mxSetM(dataref,rows); 83 83 mxSetN(dataref,1); 84 mxSetPr(dataref,vector_ ptr2);84 mxSetPr(dataref,vector_matlab); 85 85 } 86 86 else{ … … 95 95 /*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/ 96 96 void WriteData(mxArray** pdataref,double* matrix, int M,int N){ 97 98 mxArray * dataref=NULL;99 mxArray* tdataref=NULL;97 98 mxArray *dataref = NULL; 99 double *tmatrix = NULL; 100 100 101 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"); 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); 111 113 } 112 114 else{ … … 119 121 void WriteData(mxArray** pdataref,int* matrix, int M,int N){ 120 122 121 mxArray* dataref =NULL;122 mxArray* tdataref=NULL;123 mxArray* dataref = NULL; 124 double* tmatrix = NULL; 123 125 124 126 if(matrix){ 125 127 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]; 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); 129 139 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 //transpose137 mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");138 140 } 139 141 else{ … … 146 148 void WriteData(mxArray** pdataref,double* vector, int M){ 147 149 148 mxArray* dataref=NULL; 150 mxArray* dataref = NULL; 151 double* vector_matlab = NULL; 149 152 150 153 if(vector){ 151 154 152 /*data is a double* pointer. Copy into a vector: */ 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]; 153 158 dataref = mxCreateDoubleMatrix(0,0,mxREAL); 154 159 mxSetM(dataref,(mwSize)M); 155 160 mxSetN(dataref,(mwSize)1); 156 mxSetPr(dataref,vector );161 mxSetPr(dataref,vector_matlab); 157 162 } 158 163 else{
Note:
See TracChangeset
for help on using the changeset viewer.