2010-01-27 39 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; 
} 

相關問題