Changeset 12040


Ignore:
Timestamp:
04/17/12 20:14:35 (13 years ago)
Author:
Mathieu Morlighem
Message:

WriteMatlabData should always use Matlab's memory manager

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp

    r12037 r12040  
    6363        mxArray* dataref=NULL;
    6464        double*  vector_ptr=NULL;
    65         double*  vector_ptr2=NULL;
     65        double*  vector_matlab=NULL;
    6666        int      rows;
    6767       
     
    7676               
    7777                /*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];
    8080
    8181                dataref = mxCreateDoubleMatrix(0,0,mxREAL);                         
    8282                mxSetM(dataref,rows);
    8383                mxSetN(dataref,1);                                                                                         
    84                 mxSetPr(dataref,vector_ptr2);           
     84                mxSetPr(dataref,vector_matlab);           
    8585        }
    8686        else{
     
    9595/*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/
    9696void 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;
    100100               
    101101        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);
    111113        }
    112114        else{
     
    119121void WriteData(mxArray** pdataref,int* matrix, int M,int N){
    120122
    121         mxArray* dataref=NULL;
    122         mxArray* tdataref=NULL;
     123        mxArray* dataref = NULL;
     124        double*  tmatrix = NULL;
    123125
    124126        if(matrix){
    125127
    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);
    129139
    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");
    138140        }
    139141        else{
     
    146148void WriteData(mxArray** pdataref,double* vector, int M){
    147149       
    148         mxArray* dataref=NULL;
     150        mxArray* dataref       = NULL;
     151        double*  vector_matlab = NULL;
    149152
    150153        if(vector){
    151154
    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];
    153158                dataref = mxCreateDoubleMatrix(0,0,mxREAL);
    154159                mxSetM(dataref,(mwSize)M);
    155160                mxSetN(dataref,(mwSize)1);
    156                 mxSetPr(dataref,vector);
     161                mxSetPr(dataref,vector_matlab);
    157162        }
    158163        else{
Note: See TracChangeset for help on using the changeset viewer.