使用visual studio 2013读取.mat文件
現在有一個T.mat 文件需要在c++中處理然后以.mat 或是.txt形式返回
T.mat中存儲了十個cell,每個cell中會有一個不等長的數組
1.以下是相關配置過程:
參考:http://wenku.baidu.com/link?url=3CiA3T6wtaBEUSJnpvmfmvZzaKXop1Ea68HM-s1S02fMZUm5dhYsqrC4tE4JNjbYbvnflEe7ZaRa5oqCQmNGBvmAi67ZujICsjxXKIuEzC
?
VC++ 包含目錄 添加 D:\Program Files\MATLAB\R2014a\extern\include\win64;D:\Program Files\MATLAB\R2014a\extern\include;
連接器 常規 附加庫目錄 D:\Program Files\MATLAB\R2014a\extern\lib\win32\microsoft;D:\Program Files\MATLAB\R2014a\extern\lib\win64\microsoft
鏈接器 輸入 附加依賴項 libmat.lib;libmx.lib;libmex.lib;libeng.lib
配置管理器中活動解決方案平臺選擇x64
更改環境變量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];由于數組 的長度需要事先給定,所以改用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); }?
后記:這里主要還是驗證配置的正確性,只進行了一個加和運算。
修改:使用vector來存儲計算結果,可以適應數據的長度改變。
轉載于:https://www.cnblogs.com/simayuhe/p/5246157.html
總結
以上是生活随笔為你收集整理的使用visual studio 2013读取.mat文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: private-bower
- 下一篇: [原创]django+ldap+memc