现在有一个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> int main() { MATFile *pmatFile = NULL; mxArray *pMxArray = NULL, *pMxArray_i = NULL; double *pMx_cell_i = NULL; //读入.MAT pmatFile = matOpen("D:/Program Files/MATLAB/R2014a/work/mywork/test_for_data/mydata.mat", "r"); pMxArray = matGetVariable(pmatFile, "T"); 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]; //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; // } //写到e:/data.TXT中 FILE *p; if ((p = fopen("e:\\data.txt", "wt")) != NULL) for (int i = 0; i<10; i++) fprintf(p, "%d \n", A[i]); fclose(p); }
后记:这里主要还是验证配置的正确性,只进行了一个加和运算。
原文:http://www.cnblogs.com/simayuhe/p/5246157.html