2010-01-27 45 views

回答

1

的MATLAB文件格式是documented here。看起来不毛。

编辑:对不起,链接已损坏。

+0

您跟随的文档链接不再有效。 – iceman 2010-01-27 20:24:52

1

与libmx.lib,libmat.lib,libeng.lib链接,并包含头文件mat.h和engine.h。我忽略了数据的虚构成分,并假设你知道如何使用C++ STL。下面的代码是足够的,但一个叫mxWrapper容易接口可以在这里找到:http://www.mathworks.com/matlabcentral/fileexchange/28331-replacement-for-mwarray-using-matlab-engine

vector<double> readSomeNumbers() {

vector<double> data; 

    mxArray *pMx=load("c:\\someFile.mat", "foobar"); 

    if (!pMx) return data; 

    ASSERT(mxGetClassID(pMx) == mxDOUBLE_CLASS); 

    data.assign(mxGetPr(pMx), mxGetPr(pMx)+mxGetNumberOfElements(pMx)); 

    mxDestroyArray(pMx); 

    return data; 
} 

mxArray *load(const string& fileName, const string& variableName) 
{ 

    MATFile *pmatFile = matOpen(fileName.c_str(), "r"); 

    if(pmatFile == NULL) 
     return NULL; 

    mxArray* pMx = matGetVariable(pmatFile, variableName.c_str()); 

    if(pMx == NULL) 
    { 
     matClose(pmatFile); 
     return NULL; 
    } 

    matClose(pmatFile); 
    return pMx; 
} 

相关问题