search

Home  >  Q&A  >  body text

matlab - 使用vs2013c++的语法能否正确读入我的数据?

我最近使用vs2013,读取了一个由matlab导出的.txt数据文件。数据大概是这样的:

1.6526799e-01   9.6253190e-02   1.6528841e-02   2.3563042e-02   9.5957121e-02   3.8080653e-03   1.9806023e-03   4.5839714e-03   1.5982644e-01   1.9387443e-02   1.7151608e-03   9.8825932e-03   3.9407861e-03   3.0627871e-05   0.0000000e+00   9.1577335e-03   1.8204186e-01   2.4951506e-02   3.2261358e-03   7.4017356e-03   6.2419602e-02   4.3083206e-03   7.2485962e-04   7.8815722e-03   5.3935681e-02   4.8187851e-03   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+00   3.6406330e-02   4.2735043e-02   1.8803419e-02   2.2222222e-02   0.0000000e+00   1.3675214e-02   1.5384615e-02   0.0000000e+00       

是double型的,导出是科学记数法。
现在我写了这样的代码像读取它:

void returnfun(int nRows, int nDims, std::vector<double> & data)
{
    data.resize(nRows*nDims);
    std::ifstream fin("dataset1.txt");

    while (!fin.eof())
    {
        for (int i = 0; i < nRows; i++)
            for (int j = 0; j < nDims; j++)
                fin >> data[i*nDims + j];
    }
                    ······
}

nRows和nDims是这个数据原始的维度,我在其他地方有用。
可是我想输出检测一下的时候发现结果并不对···
输出的代码如下:

    for (int x = 0; x < nRows*nDims; x++)
    {
        if ((x+1)%nDims!=0)
            printf("%.8f\t", data[x]);
        else{
            printf("%.8f\t", data[x]);
            printf("\n");
        }
    }

输出的结果是:

感觉完全对不上···(2份数据都是从头截取展示的)
所以,我的问题是:
1.如何设计代码使得可以正确读入数据?
2.有没有方法像matlab那样,直接可以查看变量的情况?
问题较长,谢谢!

迷茫迷茫2767 days ago548

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 13:42:35

    1. At first glance, the reading function and output function are fine. The problem may be in the txt format. You can send the test data to my email and I will write a test example.
    email:luffygo@163.com
    2. Check the variables. You can see the variables at the break point in vs.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:42:35

    After my testing, I found that the truth is:
    The cmd window is full, and the first data is no longer the first input data... After all, I have 190,000 pieces of data...
    It makes sense to use lf as mentioned above, to ensure the completeness of the output.

    reply
    0
  • Cancelreply