// mat_reader.h // // See mat_reader_main.c for an example of how to use. // This is the header file for mat_header.c // // Description: // Search this file for "USER FUNCTION" to find the functions // and datatypes that you may use. // These files are setup to be descriptive and have many // printf statements that you can turn off by setting // DEBUG_LEVEL to 0. // // Revision history: // Nov 6, 2011: added additional commenting // // Author: John Paden #ifndef __MAT_READER_H__ #define __MAT_READER_H__ struct mat_header { char text[117]; char subsys_data_offset[9]; unsigned short version; char endian_indicator[3]; }; struct mat_data_type { unsigned int data_type; unsigned int num_bytes; }; // USER FUNCTION // This struct contains all of the header information // for one variable. Variable names are limited // to 128 bytes. Variable dimensions are limited to 2. struct mat_matrix { struct mat_data_type data_type; unsigned int array_flags[2]; int dims[2]; char name[129]; int file_offset; }; #define MAT_READER_miINT8 1 #define MAT_READER_miUINT8 2 #define MAT_READER_miINT16 3 #define MAT_READER_miUINT16 4 #define MAT_READER_miINT32 5 #define MAT_READER_miUINT32 6 #define MAT_READER_miSINGLE 7 #define MAT_READER_miDOUBLE 9 #define MAT_READER_miINT64 12 #define MAT_READER_miUINT64 13 #define MAT_READER_miMATRIX 14 #define MAT_READER_mxCHAR_CLASS 4 #define MAT_READER_mxDOUBLE_CLASS 6 #define MAT_READER_mxSINGLE_CLASS 7 #define MAT_READER_mxINT8_CLASS 8 #define MAT_READER_mxUINT8_CLASS 9 #define MAT_READER_mxINT16_CLASS 10 #define MAT_READER_mxUINT16_CLASS 11 #define MAT_READER_mxINT32_CLASS 12 #define MAT_READER_mxUINT32_CLASS 13 #define MAT_READER_mxINT64_CLASS 14 #define MAT_READER_mxUINT64_CLASS 15 #define DEBUG_LEVEL 0 // 0: none, 1: some, 2: all #define dprintf !DEBUG_LEVEL ? : printf // mat_reader_vars (USER FUNCTION) // This function returns a list of the variables in the // file that this file reader can read. Basic attributes // about the variables are also returned. Variables which // are not supported will have a zero length name in the // vars output structure. // fn = input .MAT file name // vars = output list of variables stored in an array *vars // (see structure definition in this header file for details). // This memory is allocated in the function, so should be freed // by the caller (e.g. free(vars)). // num_vars = scalar integer containing the number of variables. // Should be preallocated. int mat_reader_vars(const char *fn, struct mat_matrix **vars, int *num_vars); // mat_reader_data // Support function that reads the data from the "data" subelement int mat_reader_data(FILE *fid, int data_type, int num_bytes, int numel, double *data, int data_packed_flag); // mat_reader_tag // Support function that reads the tag for variables and subelements int mat_reader_tag(FILE *fid, unsigned int *num_bytes, unsigned int *data_type, int *data_packed); // mat_reader_get (USER FUNCTION) // Function for getting data. // fn = input .MAT file name // var_name = input, name of variable to get from the file // data = 2D matrix of data read from the file // Memory allocated in the function, so it needs to be freed // by the calling program, e.g. free(data) // dims = dimensions of the data (should be a int[2], preallocated) int mat_reader_get(const char *fn, const char *var_name, double **data, int *dims); // mat_reader_print (USER FUNCTION) // Function for printing out 2D matrix of data. // data and dims are the same as what are returned from mat_reader_get // function. void mat_reader_print(const double *data, const int dims[2]); #endif