xDouble_Float.h // xDouble.h: interface for the xDouble class. // // /************************************************************************/ /* matlab&c++ author:san address:南京航空航天大学 CEPE 202 EMAIL:visualsan@yahoo.cn This file is part of matlab&c++ interface. feel easy to use it. san. 2010.3. */ /************************************************************************/ #if !defined(AFX_XDOUBLE_H__04604EBE_C75A_4413_AFC1_CD2A58D7C346__INCLUDED_) #define AFX_XDOUBLE_H__04604EBE_C75A_4413_AFC1_CD2A58D7C346__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "ImxArray.h" #include "xMatrix.h" template<class T> class MAT_API xDouble_Float :public ImxArray { public: xDouble_Float(T valReal=0.0,T valImg=0,bool bComplex=0); virtual ~xDouble_Float(); virtual mxArray* GetArray(){ return m_pDouble->GetArray();} //real T GetRealData(); void SetRealData(T val); //img T GetImgData(); void SetImgData(T val); bool IsComplex(); virtual bool SetArray(mxArray* pArray,bool bCopy=0); private: xMatrix<T> *m_pDouble; }; template<class T> xDouble_Float<T>::xDouble_Float(T valReal,T valImg,bool bComplex) { std::string str=typeid(T).name(); printf("type=%s/n",str.data()); char *copy1=_strlwr( _strdup( str.data() ) ); str=copy1; if(strcmp(copy1,"double")==0){ m_dataType = MX_DATA_DOUBLE; }else if(strcmp(copy1,"float")==0){ m_dataType = MX_DATA_FLOAT; } strcpy(m_szTypeName,str.data()); m_pDouble = new xMatrix<T>(1,1,&valReal,&valImg,bComplex?mxCOMPLEX:mxREAL); free(copy1); } template<class T> xDouble_Float<T>::~xDouble_Float() { delete m_pDouble; } //real template<class T> T xDouble_Float<T>::GetRealData() { T val; m_pDouble->GetRealData(&val,1); return val; } template<class T> void xDouble_Float<T>::SetRealData(T val) { m_pDouble->SetRealData(&val,1,1); } //img template<class T> T xDouble_Float<T>::GetImgData() { T val; m_pDouble->GetImgData(&val,1); return val; } template<class T> void xDouble_Float<T>::SetImgData(T val) { m_pDouble->SetImgData(&val,1,1); } template<class T> bool xDouble_Float<T>::IsComplex() { return m_pDouble->IsComplex(); } template<class T> bool xDouble_Float<T>::SetArray(mxArray* pArray,bool bCopy) { return m_pDouble->SetArray(pArray,bCopy); } typedef xDouble_Float<double> xDouble; typedef xDouble_Float<float> xFloat; #endif // !defined(AFX_XDOUBLE_H__04604EBE_C75A_4413_AFC1_CD2A58D7C346__INCLUDED_) NO xDouble_Float.CPP xFile.h // xFile.h: interface for the xFile class. // // /************************************************************************/ /* matlab&c++ author:san address:南京航空航天大学 CEPE 202 EMAIL:visualsan@yahoo.cn This file is part of matlab&c++ interface. feel easy to use it. san. 2010.3. */ /************************************************************************/ #if !defined(AFX_XFILE_H__2F100C7A_3FF1_4C83_9558_20DDE047C851__INCLUDED_) #define AFX_XFILE_H__2F100C7A_3FF1_4C83_9558_20DDE047C851__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "mxWrap.h" #include <mat.h> #include <vector> #include <string> class MAT_API xFile { struct Var { char name[64]; mxWrap* pArray; bool bGobal; }; public: xFile(); xFile(char *mFile); virtual ~xFile(); bool Open(char *matFile,char* mode="u");//mode="r" 读, "u"读写 ,"w" 写,"w4","wL","wz"压缩模式 bool Close(); std::string GetFilePath(); mxWrap* GetArray(char*name,bool bCopy=1); void SetArray(char*name,mxWrap*pArray); int GetVarCount(); mxWrap* GetArray(int index,std::string*nameOut=NULL,bool bCopy=1); bool RemoveVar(char*name); bool RemoveVar(int index); void RemoveAll(); private: std::string m_filePath; MATFile *m_pFile; std::vector<Var> m_variable; bool FormatCheak(mxClassID id); }; #endif // !defined(AFX_XFILE_H__2F100C7A_3FF1_4C83_9558_20DDE047C851__INCLUDED_) xFile.cpp // xFile.cpp: implementation of the xFile class. // // /************************************************************************/ /* matlab&c++ author:san address:南京航空航天大学 CEPE 202 EMAIL:visualsan@yahoo.cn This file is part of matlab&c++ interface. feel easy to use it. san. 2010.3. */ /************************************************************************/ #include "xFile.h" // // Construction/Destruction // xFile::xFile():m_filePath(""),m_pFile(0) { } xFile::xFile(char *mFile):m_filePath(mFile),m_pFile(0) { Open(mFile); } xFile::~xFile() { RemoveAll(); } std::string xFile::GetFilePath() { return m_filePath; } bool xFile::Open(char *matFile,char* mode) { Close(); m_pFile = matOpen(matFile,mode); if(m_pFile) { const char *name; mxArray *p=matGetNextVariable(m_pFile,&name); while (p) { if(FormatCheak(mxGetClassID(p))) { xFile::Var v; strcpy(v.name,name); v.pArray = new mxWrap( p ); // printf("id=%d/n",v.pArray->GetDataType()); m_variable.push_back(v); }else mxDestroyArray(p); p=matGetNextVariable(m_pFile,&name); } } return m_pFile != NULL; } bool xFile::Close() { if(m_pFile) matClose(m_pFile); m_pFile=0; RemoveAll(); return 1; } mxWrap* xFile::GetArray(char*name,bool bCopy) { if(name[0]=='/0') return NULL; for (int i=0;i<m_variable.size();i++) { if(strcpy(name,m_variable[i].name)==0) { return bCopy?new mxWrap(*m_variable[i].pArray):m_variable[i].pArray; } } return NULL; } void xFile::SetArray(char*name,mxWrap*pArray) { if(name[0]=='/0') return ; for (int i=0;i<m_variable.size();i++) { if(strcmp(name,m_variable[i].name)==0) { m_variable[i].pArray->SetArray( pArray->GetArray(),1); if(m_pFile) matPutVariable(m_pFile,name,pArray->GetArray()); return; } } xFile::Var v; strcpy(v.name,name); v.pArray=new mxWrap(*pArray); if(m_pFile) matPutVariable(m_pFile,name,pArray->GetArray()); m_variable.push_back(v); } int xFile::GetVarCount() { return m_variable.size(); } mxWrap* xFile::GetArray(int index,std::string*name,bool bCopy) { if(index<0||index>GetVarCount()-1) return NULL; if(name) *name = m_variable[index].name; return bCopy?new mxWrap(*m_variable[index].pArray):m_variable[index].pArray; } bool xFile::RemoveVar(char*name) { if(name[0]=='/0') return NULL; for (int i=0;i<m_variable.size();i++) { if(strcmp(name,m_variable[i].name)==0) { delete m_variable[i].pArray; m_variable.erase( m_variable.begin() + i ); if(m_pFile) matDeleteVariable(m_pFile,name); return 1; } } return NULL; } bool xFile::RemoveVar(int index) { if(index<0||index>GetVarCount()-1) return NULL; delete m_variable[index].pArray; if(m_pFile) matDeleteVariable(m_pFile,m_variable[index].name); m_variable.erase( m_variable.begin() + index ); return 1; } bool xFile::FormatCheak(mxClassID id) { //支持的数据格式 return id==mxLOGICAL_CLASS|| id==mxCHAR_CLASS|| id==mxDOUBLE_CLASS|| id==mxSINGLE_CLASS|| id==mxINT32_CLASS|| id==mxUINT32_CLASS|| id==mxINT64_CLASS|| id==mxSTRUCT_CLASS|| id==mxUINT64_CLASS; } void xFile::RemoveAll() { for (int i=0;i<m_variable.size();i++) { delete m_variable[i].pArray; if(m_pFile) matDeleteVariable(m_pFile,m_variable[i].name); } m_variable.clear(); }