使用visual studio 2013读取.mat文件
現(xiàn)在有一個(gè)T.mat 文件需要在c++中處理然后以.mat 或是.txt形式返回
T.mat中存儲(chǔ)了十個(gè)cell,每個(gè)cell中會(huì)有一個(gè)不等長(zhǎng)的數(shù)組
1.以下是相關(guān)配置過(guò)程:
參考:http://wenku.baidu.com/link?url=3CiA3T6wtaBEUSJnpvmfmvZzaKXop1Ea68HM-s1S02fMZUm5dhYsqrC4tE4JNjbYbvnflEe7ZaRa5oqCQmNGBvmAi67ZujICsjxXKIuEzC
?
VC++ 包含目錄 添加 D:\Program Files\MATLAB\R2014a\extern\include\win64;D:\Program Files\MATLAB\R2014a\extern\include;
連接器 常規(guī) 附加庫(kù)目錄 D:\Program Files\MATLAB\R2014a\extern\lib\win32\microsoft;D:\Program Files\MATLAB\R2014a\extern\lib\win64\microsoft
鏈接器 輸入 附加依賴項(xiàng) libmat.lib;libmx.lib;libmex.lib;libeng.lib
配置管理器中活動(dòng)解決方案平臺(tái)選擇x64
更改環(huán)境變量path D:\Program Files\MATLAB\R2014a\extern\lib\win64\microsoft;D:\Program Files\MATLAB\R2014a\bin\win64
2.然后是讀入的代碼
//read .mat from matlab #include <iostream> #include <mat.h> #include<iomanip> #include <fstream> #include <vector> using std::vector; int main() {MATFile *pmatFile = NULL;mxArray *pMxArray = NULL, *pMxArray_i = NULL;double *pMx_cell_i = NULL;//讀入.MATpmatFile = matOpen("D:/Program Files/MATLAB/R2014a/work/mywork/test_for_data/mydata1.mat", "r");//std::cout << pmatFile;pMxArray = matGetVariable(pmatFile, "w");//size_t M = mxGetM(pMxArray);//size_t N = mxGetN(pMxArray);//std::cout << N;int num_of_cell{ (int)mxGetNumberOfElements(pMxArray) };std::cout << "the number of the CELL we have got for the T :" << num_of_cell << std::endl;//int A[10];由于數(shù)組 的長(zhǎng)度需要事先給定,所以改用vectorvector<int> V_A;//Matrix<double> A(num_of_cell, 1);for (int i{ 0 }; i < num_of_cell; i++){pMxArray_i = mxGetCell(pMxArray, i);int num_of_cell_i = (int)mxGetNumberOfElements(pMxArray_i);std::cout << "the number of the CELL " << i << " we have got for the T :" << num_of_cell_i << std::endl;double sum_i = 0;for (int j{ 0 }; j < num_of_cell_i; j++){pMx_cell_i = mxGetPr(pMxArray_i);std::cout << std::setw(10) << *(pMx_cell_i + j);sum_i = sum_i + *(pMx_cell_i + j);}std::cout << std::endl;std::cout << "sum " << i << " is " << sum_i << std::endl;//A[i] = sum_i; V_A.push_back(sum_i);// }matClose(pmatFile);//mxFree(pMxArray);//mxFree(pMxArray_i);//mxFree(pMx_cell_i);//寫到e:/data.TXT中 FILE *p=NULL;//if ((p = fopen("e:\\data.txt", "wt")) != NULL)//for (int i = 0; i<10; i++) // fprintf(p, "%d \n", A[i]); //fclose(p);//FILE *p;if ((p = fopen("e:\\data_V.txt", "wt")) != NULL)for (int i = 0; i<num_of_cell; i++)fprintf(p, "%d \n", V_A.at(i));fclose(p); }?
后記:這里主要還是驗(yàn)證配置的正確性,只進(jìn)行了一個(gè)加和運(yùn)算。
修改:使用vector來(lái)存儲(chǔ)計(jì)算結(jié)果,可以適應(yīng)數(shù)據(jù)的長(zhǎng)度改變。
轉(zhuǎn)載于:https://www.cnblogs.com/simayuhe/p/5246157.html
總結(jié)
以上是生活随笔為你收集整理的使用visual studio 2013读取.mat文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: private-bower
- 下一篇: [原创]django+ldap+memc