c语言读取mat文件

读取含有IQ数据的mat文件

前提是导入matlab文件读写库matio.h

添加静态库libmatio.lib、hdf5.lib、szip.lib、zlib1.lib 下载地址

#include

#include "matio.h"
#pragma comment(lib,"libmatio.lib")
#pragma comment(lib,"hdf5.lib")
#pragma comment(lib,"szip.lib")
#pragma comment(lib,"zlib1.lib")

int main(){
	int i=0,j=0;
	mat_t* matfp;
	const char *file;	
	file="data_double_300000.mat";

	
	matfp=Mat_Open(file, MAT_ACC_RDONLY);
	if (NULL == matfp) {
         printf("Error opening MAT file %s\n", file);
         return -1;
    }

	matvar_t* matvar_I=NULL;
	matvar_t* matvar_Q=NULL;
	matvar_I=Mat_VarRead(matfp,"I");
	matvar_Q=Mat_VarRead(matfp,"Q");
	if(!matvar_I)
	{
		printf("Error read MatVar: I.\n");
		return -1;
	}
	if(!matvar_Q)
	{
		printf("Error read MatVar: Q.\n");
		return -1;
	}

	int iDataN=matvar_I->nbytes/matvar_I->data_size;
	double* dI=new double[iDataN];
	double* dQ=new double[iDataN];
	memset(dI,0,iDataN*sizeof(double));
	memset(dQ,0,iDataN*sizeof(double));

    //IQ数据是double类型
	if (matvar_I->data_type == MAT_T_DOUBLE){
		const double* xData_I=static_cast(matvar_I->data);	
		const double* xData_Q=static_cast(matvar_Q->data);
		memcpy(dI,xData_I,iDataN*sizeof(double));
		memcpy(dQ,xData_Q,iDataN*sizeof(double));
    
    //IQ数据是short类型
	}else if(matvar_I->data_type == MAT_T_INT16){
		const short* xData_I=static_cast(matvar_I->data);	
		const short* xData_Q=static_cast(matvar_Q->data);

		for(i=0; i(matvar_fs->data);	
	const double *startfre=static_cast(matvar_startfre->data);	
	const double *stopfre=static_cast(matvar_stopfre->data);	

	double fs=*dfSample;
	double Bandw=fs/2;
	double Startfre=*startfre, Stopfre=*stopfre;
	double band=Stopfre-Startfre;
	double F_c=(Stopfre+Startfre)/2;
	double iGain=0,iAtt=0;

	
	
	Mat_VarFree(matvar_I);
	Mat_VarFree(matvar_Q);
	Mat_VarFree(matvar_fs);
	Mat_VarFree(matvar_startfre);
	Mat_VarFree(matvar_stopfre);
	Mat_Close(matfp);


	if (iDataN < 10000)
	{
		return -1;
	}

    if(dI != NULL)
	{
		delete []dI;
		dI = NULL;
	}

	if(dQ != NULL)
	{
		delete []dQ;
		dQ = NULL;
	}


    return 0;

}

另外:在VS工程中,添加c/c++工程中外部头文件(.h),lib库,dll库的基本步骤

你可能感兴趣的:(7.,c/c++,5.,信号与信息处理,c语言,mat文件)