0

I have a large csv-file, which looks similar to the following:

enter image description here

My problem is related to a question previously posted and superbly answered by @gnovice. I am using the folling code to format the data.

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];

The output, however looks like:

enter image description here

It should ideally look like:

enter image description here

Whatis the part of the code that I have to adjust?

2 Answers 2

1

Your date appears in the second column for this data, whereas it appeared in the third column on the previous question. Just change the index in the second line to a 2:

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

And change the index for the accumulated data to a 3:

A = accumarray([rowIndex colIndex], data(:, 3), [], @(x) x(1));
Sign up to request clarification or add additional context in comments.

3 Comments

The solution for this setting works perfectly well. Although not directly related to this question, could you please explain, why I obtain an error "Error using dlmread" - "Empty format string is not supported at the end of a file."? The real data are far larger, yet the csv-file looks the same as in the example above?
@Joe: Not sure. I couldn't find a way to recreate it. I'd check the last line of your file for extra whitespace.
Saving the large csv-file (1.875.150 KB), the data seem to get lost as the file then has 0 KB. I fixed it and your code performs very quick!
1

Another way would be to use "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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.