在 MATLAB 中格式化来自 csv 文件的数据

Formatting data from a csv file in MATLAB

我有一个很大的 csv-file,它看起来类似于以下内容:

我的问题与之前发布的 相关,@gnovice 的回答非常出色。我正在使用以下代码来格式化数据。

data = csvread('datacsv.csv',1,0);
[rowVals, ~, rowIndex] = unique(data(:, 3));
[colVals, ~, colIndex] = unique(data(:, 1).');
A = accumarray([rowIndex colIndex], data(:, 2), [], @(x) x(1));  % Keeps the first value
A = [NaN colVals; rowVals A];

输出看起来像:

它应该看起来像:

我必须调整的代码部分是什么?

你的日期出现在这个数据的第二列,而它出现在上一个问题的第三列。只需将第二行中的索引更改为 2:

[rowVals, ~, rowIndex] = unique(data(:, 2));

并将累积数据的索引更改为 3:

A = accumarray([rowIndex colIndex], data(:, 3), [], @(x) x(1));

另一种方法是使用 "unstack":

myTable = readtable( 'data.csv' );
newTable = unstack( myTable, 'C', 'A' )

newTable =

  3×3 table

        B          x1         x2   
    _________    _______    _______

    2.015e+07        NaN       -2.5
    2.016e+07    -2.5625       -2.5
    2.017e+07        2.5    -2.5625