zoukankan      html  css  js  c++  java
  • 使用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 FilesMATLABR2014aexternincludewin64;D:Program FilesMATLABR2014aexterninclude;

    连接器 常规 附加库目录 D:Program FilesMATLABR2014aexternlibwin32microsoft;D:Program FilesMATLABR2014aexternlibwin64microsoft

    链接器 输入 附加依赖项 libmat.lib;libmx.lib;libmex.lib;libeng.lib

    配置管理器中活动解决方案平台选择x64

    更改环境变量path D:Program FilesMATLABR2014aexternlibwin64microsoft;D:Program FilesMATLABR2014ainwin64

    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;
        //读入.MAT
        pmatFile = 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];由于数组 的长度需要事先给定,所以改用vector
        vector<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 
    ", 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 
    ", V_A.at(i));
        fclose(p);
    }


    后记:这里主要还是验证配置的正确性,只进行了一个加和运算。

    修改:使用vector来存储计算结果,可以适应数据的长度改变。

  • 相关阅读:
    浅谈“踹”字典树
    2019.11.9 csp-s 考前模拟
    Luogu P1600[NOIP2016]day1 T2天天爱跑步
    Luogu P1197 [JSOI2008]星球大战
    Trie树(字典树)整理
    洛谷P3294 [SCOI2016]背单词——题解
    浅析差分及其推广(树上差分与广义差分)
    洛谷P1600 天天爱跑步——题解
    深入理解容斥原理
    花式求LCA
  • 原文地址:https://www.cnblogs.com/simayuhe/p/5246157.html
Copyright © 2011-2022 走看看