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);
}
Example #2
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;
}
int sci_matfile_varreadnext(char *fname, void* pvApiCtx)
{
    mat_t *matfile = NULL;
    matvar_t *matvar = NULL;
    int fileIndex = 0;
    int returnedClass = 0, var_type;
    int * fd_addr = NULL;
    double tmp_dbl;
    SciErr sciErr;

    CheckRhs(1, 1);
    CheckLhs(1, 3);

    /* Input argument is the index of the file to read */

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &fd_addr);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    sciErr = getVarType(pvApiCtx, fd_addr, &var_type);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if (var_type == sci_matrix)
    {
        getScalarDouble(pvApiCtx, fd_addr, &tmp_dbl);
        if (!isScalar(pvApiCtx, fd_addr))
        {
            Scierror(999, _("%s: Wrong size for first input argument: Single double expected.\n"), fname);
            return FALSE;
        }
        fileIndex = (int)tmp_dbl;
    }
    else
    {
        Scierror(999, _("%s: Wrong type for first input argument: Double expected.\n"), fname);
        return FALSE;
    }

    /* Gets the corresponding matfile */
    matfile_manager(MATFILEMANAGER_GETFILE, &fileIndex, &matfile);

    if (matfile == NULL)
    {
        Scierror(999, _("%s: Invalid file identifier.\n"), fname);
        return FALSE;
    }

    matvar = Mat_VarReadNext(matfile);
    if ((matvar == NULL) || (matvar->name == NULL))
    {
        /* Return empty name */
        createSingleString(pvApiCtx, Rhs + 1, "\0");
        LhsVar(1) = Rhs + 1;

        if (Lhs >= 2)
        {
            /* Return empty value */
            createEmptyMatrix(pvApiCtx, Rhs + 2);
            LhsVar(2) = Rhs + 2;
        }

        if (Lhs == 3)
        {
            /* Return error flag instead of variable class */
            createScalarDouble(pvApiCtx, Rhs + 3, NO_MORE_VARIABLES);
            LhsVar(3) = Rhs + 3;
        }

        PutLhsVar();

        return TRUE;
    }

    /* To be sure isComplex is 0 or 1 */
    matvar->isComplex =  matvar->isComplex != 0;

    /* Return the variable name */
    createSingleString(pvApiCtx, Rhs + 1, matvar->name);
    LhsVar(1) = Rhs + 1;

    returnedClass = matvar->class_type;

    if (Lhs >= 2)
    {
        /* Return the values */
        if (!CreateMatlabVariable(pvApiCtx, Rhs + 2, matvar, NULL, -1)) /* Could not Create Variable */
        {
            sciprint("Do not know how to read a variable of class %d.\n", matvar->class_type);
            returnedClass = UNKNOWN_VARIABLE_TYPE;
        }
        LhsVar(2) = Rhs + 2;
    }

    if (Lhs == 3)
    {
        /* Create class return value */
        createScalarDouble(pvApiCtx, Rhs + 3, returnedClass);
        LhsVar(3) = Rhs + 3;
    }

    Mat_VarFree(matvar);
    PutLhsVar();
    return TRUE;
}
Example #4
0
/** \brief read a MATLAB-array from a .mat file.
	 
	 \todo Currently, the returned array is always DOUBLE.

	 \param file the .mat file
	 \param varname the name of the variable in the .mat file; can be NULL, in this case the
	       first variable is read.
	 \return the Array-struct or NULL in case an error occurred
 */
Array* read_array_matlab( const char *file, const char *varname ){ 
  mat_t *mfile;
  matvar_t *marr=NULL;

  dprintf("Reading variable '%s' from file: '%s'\n", varname, file);
  mfile = Mat_Open( file, MAT_ACC_RDONLY);
  if( !mfile ){
	 errprintf("Error opening MATLAB file '%s'\n", file );
	 return NULL;
  }

  if( varname ){
	 marr = Mat_VarRead( mfile, varname );
  } else {
	 marr = Mat_VarReadNext( mfile );
  }
  Mat_Close( mfile );
  dprintf("Done\n");

  if( !marr ){
	 errprintf("Something is wrong, could not read variable\n");
	 return NULL;
  }
  Array *out=array_new( DOUBLE, marr->rank, marr->dims );

  ulong i; 
  uint *index=(uint*)malloc( out->ndim*sizeof(uint));
  for( i=0; i<marr->nbytes/marr->data_size; i++ ){
	 array_calc_colindex( i, out->size, out->ndim, index );
	 
	 switch( marr->data_type ){
	 case MAT_T_INT8:
		*((double*)array_index(out,index))=(double)(*(int8_t*)(marr->data+(i*marr->data_size))); 
		break;
	 case MAT_T_UINT8:
		*((double*)array_index(out,index))=(double)(*(uint8_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_INT16:
		*((double*)array_index(out,index))=(double)(*(int16_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_UINT16:
		*((double*)array_index(out,index))=(double)(*(uint16_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_INT32:
		*((double*)array_index(out,index))=(double)(*(int32_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_UINT32:
		*((double*)array_index(out,index))=(double)(*(uint32_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_INT64:
		*((double*)array_index(out,index))=(double)(*(int64_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_UINT64:
		*((double*)array_index(out,index))=(double)(*(uint64_t*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_SINGLE:
		*((double*)array_index(out,index))=(double)(*(float*)(marr->data+(i*marr->data_size)));
		break;
	 case MAT_T_DOUBLE:
		*((double*)array_index(out,index))=(double)(*(double*)(marr->data+(i*marr->data_size)));
		break;
	 default:
		errprintf("Unknown Data-Type in MATLAB-file!\n");
		break;
	 }
  }
  free(index);
  
  return out;
}