Exemplo n.º 1
0
void readHeightAndTopSurfaceFromMATLABFile(const char* filename, CpuPtr_2D& H, CpuPtr_2D& top_surface, int& nx, int& ny){

mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(filename, MAT_ACC_RDONLY);
if ( NULL == matfp ) {
	fprintf(stderr,"Error opening MAT file");
}

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
nx = matvar->dims[0];
ny = matvar->dims[1];
int size = nx*ny;
H = CpuPtr_2D(nx, ny, 0, true);
memcpy(H.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
top_surface = CpuPtr_2D(nx, ny, 0, true);
memcpy(top_surface.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;
}
void extra_matio_print_all_var_info_clumsy(mat_t * matfp) {
  matvar_t *matvar;
  size_t    nbytes;
  int       i;
  char size[32] = {' ',};
  fflush(stdout);
  printf( "%-20s       %-10s     %-10s     %-18s\n", "Name", "Size", "Bytes", "Class");
  while ( NULL != (matvar = Mat_VarReadNextInfo(matfp)) ) {
    printf("%-20s", matvar->name);
    if ( matvar->rank > 0 ) {
      int cnt = 0;
      printf("%8d", matvar->dims[0]);
      for ( i = 1; i < matvar->rank; i++ ) {
        if ( ceil(log10(matvar->dims[i]))+1 < 32 )
          cnt += sprintf(size+cnt,"x%d", matvar->dims[i]);
      }
      printf("%-10s",size);
    } else {
      printf("                    ");
    }
    nbytes = Mat_VarGetSize(matvar);
    printf("  %8d",nbytes);
    printf("         %-18s\n",mxclass[matvar->class_type-1]);
    Mat_VarPrint(matvar,0);
    Mat_VarFree(matvar);
  }
  fflush(stdout);
}
Exemplo n.º 3
0
      int MatFileIO::getNumberOfVariables() {
	
	Mat_Rewind(mat); // get back to first variable
	int count = 0;
	matvar_t * matvar = Mat_VarReadNextInfo(mat);
	
	while (matvar != NULL) {
	  
	  count++;
	  matvar = Mat_VarReadNextInfo(mat);
	}
	
	Mat_VarFree(matvar);
	
	return count;
      }
void extra_matio_print_all_var_info (mat_t * matfp, int printdata) {
  matvar_t *matvar;
  fflush(stdout);
  if (printdata)
    while ( NULL != (matvar = Mat_VarReadNext(matfp)) ) {
      Mat_VarPrint(matvar,printdata);
      Mat_VarFree(matvar);
    }
  else 
    while ( NULL != (matvar = Mat_VarReadNextInfo(matfp)) ) {
      Mat_VarPrint(matvar,printdata);
      Mat_VarFree(matvar);
    }
  fflush(stdout);
}
Exemplo n.º 5
0
 TData         read_data()
 {
   const char * fileName = "../data/mnist_uint8.mat";
   TData res;
   mat_t *mat = nullptr;
   scope_guard _l([&mat, fileName](){
     mat = Mat_Open(fileName, MAT_ACC_RDONLY);
     },
   [&mat](){Mat_Close(mat);});
   if(!mat){
     std::cout <<"cannot open "<<fileName<<std::endl;
     return res;
   }  
   matvar_t *var = 0;
   int read_items = 0;
   while((var = Mat_VarReadNextInfo(mat)) != nullptr){
     if(strcmp(var->name, "train_x") == 0)
     {
       res.train_x = read_matrix_in_mat(mat, var->name);
       read_items ++ ;
     }
     else if(strcmp(var->name, "train_y") == 0)
     {
       res.train_y = read_matrix_in_mat(mat, var->name);
       read_items ++;
     }
     else if(strcmp(var->name, "test_x") == 0)
     {
       res.test_x = read_matrix_in_mat(mat, var->name);
       read_items ++;
     }
     else if(strcmp(var->name, "test_y" ) == 0)
     {
       res.test_y = read_matrix_in_mat(mat, var->name);
       read_items ++;
     }
     else{
       //nothing here, just for fun...
     }
     Mat_VarFree(var);
   }
   if(read_items != 4)
   {
     std::cout<<"ooooooops, cannot read "<< fileName<<std::endl;
   }
   return res;
 }
Exemplo n.º 6
0
static Read *
read_new( const char *filename, VipsImage *out )
{
	Read *read;

	if( !(read = VIPS_NEW( NULL, Read )) )
		return( NULL );

	read->filename = vips_strdup( NULL, filename );
	read->out = out;
	read->mat = NULL;
	read->var = NULL;

	if( !(read->mat = Mat_Open( filename, MAT_ACC_RDONLY )) ) {
		vips_error( "mat2vips", 
			_( "unable to open \"%s\"" ), filename );
		read_destroy( read );
		return( NULL );
	}

	for(;;) {
		if( !(read->var = Mat_VarReadNextInfo( read->mat )) ) {
			vips_error( "mat2vips", 
				_( "no matrix variables in \"%s\"" ), 
				filename );
			read_destroy( read );
			return( NULL );
		}

#ifdef DEBUG
		printf( "mat2vips: seen:\n" );
		printf( "var->name == %s\n", read->var->name );
		printf( "var->class_type == %d\n", read->var->class_type );
		printf( "var->rank == %d\n", read->var->rank );
#endif /*DEBUG*/

		/* Vector to colour image is OK for us.
		 */
		if( read->var->rank >= 1 && read->var->rank <= 3 )
			break;

		VIPS_FREEF( Mat_VarFree, read->var );
	}

	return( read );
}
Exemplo n.º 7
0
void readPermFromMATLABFile(const char* filename, CpuPtr_3D& perm3D){

mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(filename, MAT_ACC_RDONLY);
if ( NULL == matfp ) {
	fprintf(stderr,"Error opening MAT file");
}

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
int nx = matvar->dims[0];
int ny = matvar->dims[1];
int nz = matvar->dims[2];
nz = 20;
int size = nx*ny*nz;
perm3D = CpuPtr_3D(nx, ny, nz, 0, true);
memcpy(perm3D.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;
}
Exemplo n.º 8
0
int
main (int argc, char *argv[])
{
    char *prog_name = "matdump";
    int   i, c, err = EXIT_SUCCESS;
    mat_t    *mat;
    matvar_t *matvar;
    int version[3];

    Mat_GetLibraryVersion(version, version+1, version+2);
    if ( MATIO_MAJOR_VERSION != version[0] ||
         MATIO_MINOR_VERSION != version[1] ||
         MATIO_RELEASE_LEVEL != version[2] ) {
        fprintf(stderr,"matio version in header does not match runtime "
                "version\n");
        return EXIT_FAILURE;
    }

    Mat_LogInitFunc(prog_name,default_printf_func);

    printfunc = print_default;

    while ((c = getopt_long(argc,argv,optstring,options,NULL)) != EOF) {
        switch (c) {
            case 'd':
                printdata = 1;
                Mat_VerbMessage(1,"Printing data\n");
                break;
            case 'f':
                if ( NULL != optarg && !strcmp(optarg,"whos") ) {
                    printfunc = print_whos;
                    break;
                }
                Mat_Warning("%s is not a recognized output format. "
                              "Using default\n", optarg);
                break;
            case 'h':
                human_readable = 1;
                break;
            case 'v':
                Mat_SetVerbose(1,0);
                break;
            case 'H':
                Mat_Help(helpstr);
                exit(EXIT_SUCCESS);
            case 'V':
                printf("%s %s\nWritten by Christopher Hulbert\n\n"
                       "Copyright(C) 2006-2013 Christopher C. Hulbert\n",
                       prog_name,MATIO_VERSION_STR);
                exit(EXIT_SUCCESS);
            default:
                printf("%c not a valid option\n", c);
                break;
        }
    }

    if ( (argc-optind) < 1 )
        Mat_Error("Must specify at least one argument");

    mat = Mat_Open( argv[optind],MAT_ACC_RDONLY );
    if ( NULL == mat ) {
        Mat_Error("Error opening %s\n", argv[optind]);
        return EXIT_FAILURE;
    }

    optind++;

    if ( optind < argc ) {
        /* variables specified on the command line */
        for ( i = optind; i < argc; i++ ) {
            char *next_tok_pos, next_tok = 0;

            next_tok_pos = get_next_token(argv[i]);
            if ( next_tok_pos != argv[i] ) {
                next_tok = *next_tok_pos;
                *next_tok_pos = '\0';
            }

            matvar = Mat_VarReadInfo(mat,argv[i]);
            if ( matvar ) {
                if ( printdata ) {
                    if ( next_tok == '\0' ) {
                        /* No indexing tokens found, so read all of the data */
                        Mat_VarReadDataAll(mat,matvar);
                    } else {
                        *next_tok_pos = next_tok;
                        read_selected_data(mat,matvar,next_tok_pos);
                    }
                }
                (*printfunc)(matvar);
                Mat_VarFree(matvar);
                matvar = NULL;
            } else {
                Mat_Warning("Couldn't find variable %s in the MAT file",
                      argv[i]);
            }
        } /* for ( i = optind; i < argc; i++ ) */
    } else {
        /* print all variables */
        if ( printdata ) {
            while ( (matvar = Mat_VarReadNext(mat)) != NULL ) {
                (*printfunc)(matvar);
                Mat_VarFree(matvar);
                matvar = NULL;
            }
        } else {
            while ( (matvar = Mat_VarReadNextInfo(mat)) != NULL ) {
                (*printfunc)(matvar);
                Mat_VarFree(matvar);
                matvar = NULL;
            }
        }
    }

    Mat_Close(mat);

    Mat_LogClose();

    return err;
}
Exemplo n.º 9
0
// load a .mat matlab matrix
// will load the first matrix (sparse or dense) in the file
// returns 0 on success
static
  int read_mat(char const *const filename,
               matrix_t *const A) {
#ifndef HAVE_MATIO
  return 1;
#else
  const int LOCAL_DEBUG = 0;
  mat_t* matfp;
  matfp = Mat_Open(filename, MAT_ACC_RDONLY);
  if(matfp == NULL)
    return 1; // failed to open file

  matvar_t* t;
  int more_data = 1;

  while (more_data) {
    t = Mat_VarReadNextInfo(matfp);
    if(t == NULL) {
      return 2; // no suitable variable found
    }
// TODO decide if this is the one:    if(t->
    if(1) {
      more_data = 0;
    }
    else { // keep going
      Mat_VarFree(t);
      t = NULL;
    }
  }

  { // load the selected variable, including data
    Mat_Rewind(matfp);
    matvar_t* tt = Mat_VarRead(matfp, t->name);
    Mat_VarFree(t);
    t = tt;
    if(LOCAL_DEBUG) Mat_VarPrint(tt, 1);
  }

  // debug info
  if(LOCAL_DEBUG && t->name != NULL)
    printf("%s: loaded variable %s\n", filename, t->name);

  // checks and data handling
  int ret = 0;

  if(t->rank > 2 || t->rank <= 0) { // number of dimensions
    ret = 2;
  }
  else if(t->data_type != MAT_T_DOUBLE) {
    if(LOCAL_DEBUG) printf("data_type=%d\n",t->data_type);
    ret = 3;
  }
  else if(t->isComplex) {
    ret = 10; // TODO can't handle complex matrices yet
  }
  else if(t->isLogical) {
    ret = 11; // TODO can't handle logicals yet
  }
  else {
    if(t->rank == 1) {
      A->m = t->dims[0]; // rows
      A->n = 1; // cols
    }
    else {
      A->m = t->dims[0]; // rows
      A->n = t->dims[1]; // cols
    }
    A->sym = SM_UNSYMMETRIC;
    //if(t->data_type == MAT_T_DOUBLE) {
    A->data_type = REAL_DOUBLE;
    //} // TODO complex, single precision, various sized integers

    if(t->class_type == MAT_C_SPARSE) { // t.data = sparse_t in CSC format
      // Note that Matlab will save('-v4'...) a sparse matrix
      // to version 4 format without complaint but it appears
      // to be gibberish as far as MatIO is concerned
      sparse_t* st = t->data;
      A->nz = st->ndata; //st->nzmax has the actual size of the allocated st->data
      A->format = SM_CSC;
      // transfer the data pointer into our strucut
      // TODO check for negative values in ir/jc before throwing away their signs
      A->ii = (unsigned int*) st->ir;
      st->ir = NULL;
      A->jj = (unsigned int*) st->jc;
      st->jc = NULL;
      A->dd = st->data;
      st->data = NULL;
    }
    else if(t->class_type == MAT_C_DOUBLE) {
      A->nz = A->m * A->n;
      A->format = DCOL;
      // transfer the data pointer into our struct
      A->dd = t->data;
      t->data = NULL;
    }
    else {
      ret = 4; // unknown class of data structure
    }
  }

  // DEBUG
  if(LOCAL_DEBUG && validate_matrix(A) != 0) {
    ret = 50;
    fprintf(stderr, "problem loading .mat matrix file\n");
    printf_matrix("  ", A);
  }

  // sanity checks
  // t.dims[] is don't-care
  // t.isGlobal is don't-care

  Mat_Close(matfp);
  Mat_VarFree(t);
  return ret; // success?
#endif
}
Exemplo n.º 10
0
void readFormationDataFromMATLABFile(const char* filename, CpuPtr_2D& H, CpuPtr_2D& top_surface, CpuPtr_2D& h,
									CpuPtr_2D& z_normal, CpuPtr_3D& perm3D, float* poro3D, CpuPtr_2D& pv,
									CpuPtr_2D& north_flux, CpuPtr_2D& east_flux,
									CpuPtr_2D& north_grav, CpuPtr_2D& east_grav,
									float& dz, int&nz, int& nx, int& ny){

mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(filename, MAT_ACC_RDONLY);
if ( NULL == matfp ) {
	fprintf(stderr,"Error opening MAT file");
}

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
nz = *(float*)matvar->data;
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
nx = matvar->dims[0];
ny = matvar->dims[1];
int size = nx*ny;
H = CpuPtr_2D(nx, ny, 0, true);
memcpy(H.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
h = CpuPtr_2D(nx, ny, 0, true);
memcpy(h.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
top_surface = CpuPtr_2D(nx, ny, 0, true);
memcpy(top_surface.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
pv = CpuPtr_2D(nx, ny, 0, true);
memcpy(pv.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
east_flux = CpuPtr_2D(nx, ny, 1, true);
memcpy(east_flux.getPtr(), matvar->data,sizeof(float)*(nx+1)*(ny+1));
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
north_flux = CpuPtr_2D(nx, ny, 1, true);
memcpy(north_flux.getPtr(), matvar->data,sizeof(float)*(nx+1)*(ny+1));
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
z_normal = CpuPtr_2D(nx, ny, 0, true);
memcpy(z_normal.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

//perm3D = CpuPtr_3D(nx, ny, nz, 0, true);
matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
nx = matvar->dims[0];
ny = matvar->dims[1];
nz = matvar->dims[2];
printf("3D dims: nx %i ny %i nz %i\n", nx, ny, nz);
//memcpy(perm3D.getPtr(), matvar->data,sizeof(float)*nx*ny*nz);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
//poro3D = CpuPtr_3D(nx, ny, nz, 0, true);
memcpy(poro3D, matvar->data,sizeof(float)*nx*ny*nz);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
dz = *(float*)matvar->data;
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
north_grav = CpuPtr_2D(nx, ny, 0, true);
memcpy(north_grav.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

matvar = Mat_VarReadNextInfo(matfp);
Mat_VarReadDataAll(matfp, matvar);
printf("Variable: %s\n",matvar->name);
east_grav = CpuPtr_2D(nx, ny, 0, true);
memcpy(east_grav.getPtr(), matvar->data,sizeof(float)*size);
Mat_VarFree(matvar);
matvar = NULL;

}