Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing .csv data to matrix

I have a .csv file with the following 'configuration'

'string', 'string', 'string', 'string', 'string'
'string', 'string', 21, 89, 67
'string', 'string', 45, 12, -16
'string', 'string', 78, 56, 45
'string', 'string', 23, 65, 90
'string', 'string', 43, 34, 75

I would like to ignore the first two columns and the first row, and import the numbers to a matrix.

I have tried using textscan, but without luck. Any experts? :-)

like image 628
Johnny Doe Avatar asked May 27 '26 08:05

Johnny Doe


2 Answers

Try dlmread. You can specify the row and column to start the import.

data = dlmread('test.txt',',',1,2)

data =

    21    89    67
    45    12   -16
    78    56    45
    23    65    90
    43    34    75
like image 57
Nemesis Avatar answered May 30 '26 12:05

Nemesis


Use importdata:

x = importdata('filename.csv');

This gives an x struct with data and textdata fields:

>> x
x = 
        data: [5x3 double]
    textdata: {6x5 cell}

To get only the numeric values, use

x_numeric = x.data;

In your example, this gives

x_numeric =
    21    89    67
    45    12   -16
    78    56    45
    23    65    90
    43    34    75
like image 28
Luis Mendo Avatar answered May 30 '26 12:05

Luis Mendo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!