Index: /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12039)
+++ /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12040)
@@ -63,5 +63,5 @@
 	mxArray* dataref=NULL;
 	double*  vector_ptr=NULL;
-	double*  vector_ptr2=NULL;
+	double*  vector_matlab=NULL;
 	int      rows;
 	
@@ -76,11 +76,11 @@
 		
 		/*now create the matlab vector with Matlab's memory manager */
-		vector_ptr2=(double*)mxMalloc(rows*sizeof(double));
-		for(int i=0;i<rows;i++) vector_ptr2[i]=vector_ptr[i];
+		vector_matlab=(double*)mxMalloc(rows*sizeof(double));
+		for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
 
 		dataref = mxCreateDoubleMatrix(0,0,mxREAL);                         
 		mxSetM(dataref,rows);
 		mxSetN(dataref,1);                                                                                          
-		mxSetPr(dataref,vector_ptr2);           
+		mxSetPr(dataref,vector_matlab);           
 	}
 	else{
@@ -95,18 +95,20 @@
 /*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/
 void WriteData(mxArray** pdataref,double* matrix, int M,int N){
-	
-	mxArray* dataref=NULL;
-	mxArray* tdataref=NULL;
+
+	mxArray *dataref  = NULL;
+	double  *tmatrix  = NULL;
 		
 	if(matrix){
-		
-		/*data is a double* pointer. Copy into a matrix: */
-		tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
-		mxSetM(tdataref,(mwSize)N);
-		mxSetN(tdataref,(mwSize)M);
-		mxSetPr(tdataref,(double*)matrix);
-
-		//transpose
-		mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
+		/*create the matlab matrixwith Matlab's memory manager */   
+		tmatrix=(double*)mxMalloc(M*N*sizeof(double));
+		for(int i=0;i<M;i++){
+			for(int j=0;j<N;j++){
+				tmatrix[i*N+j]=matrix[j*M+i];
+			}
+		}
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(dataref,(mwSize)N);
+		mxSetN(dataref,(mwSize)M);
+		mxSetPr(dataref,(double*)tmatrix);
 	}
 	else{
@@ -119,21 +121,21 @@
 void WriteData(mxArray** pdataref,int* matrix, int M,int N){
 
-	mxArray* dataref=NULL;
-	mxArray* tdataref=NULL;
+	mxArray* dataref = NULL;
+	double*  tmatrix = NULL;
 
 	if(matrix){
 
-		/*convert to double matrix*/
-		double* doublematrix=(double*)mxMalloc(M*N*sizeof(double));
-		for(int i=0;i<M*N;i++) doublematrix[i]=(double)matrix[i];
+		/*convert to double matrix using Matlab's memory manager*/
+		double* tmatrix=(double*)mxMalloc(M*N*sizeof(double));
+		for(int i=0;i<M;i++){
+			for(int j=0;j<N;j++){
+				tmatrix[i*N+j]=(double)matrix[j*M+i];
+			}
+		}
+		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
+		mxSetM(dataref,(mwSize)N);
+		mxSetN(dataref,(mwSize)M);
+		mxSetPr(dataref,(double*)tmatrix);
 
-		/*data is a double* pointer. Copy into a matrix: */
-		tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
-		mxSetM(tdataref,(mwSize)N);
-		mxSetN(tdataref,(mwSize)M);
-		mxSetPr(tdataref,(double*)doublematrix);
-
-		//transpose
-		mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
 	}
 	else{
@@ -146,13 +148,16 @@
 void WriteData(mxArray** pdataref,double* vector, int M){
 	
-	mxArray* dataref=NULL;
+	mxArray* dataref       = NULL;
+	double*  vector_matlab = NULL;
 
 	if(vector){
 
-		/*data is a double* pointer. Copy into a vector: */
+		/*create the matlab vector with Matlab's memory manager */
+		vector_matlab=(double*)mxMalloc(M*sizeof(double));
+		for(int i=0;i<M;i++) vector_matlab[i]=vector[i];
 		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
 		mxSetM(dataref,(mwSize)M);
 		mxSetN(dataref,(mwSize)1);
-		mxSetPr(dataref,vector);
+		mxSetPr(dataref,vector_matlab);
 	}
 	else{
