6

I am trying to read a CSV file in matlab. I just want to read the second column but the code below prints out everything on CSV file. What parameters or functions I have to introduce to make it read just the second column

FILENAME = 'C:\Users\Desktop\Results.csv';

fid = fopen(FILENAME, 'rt');
a = textscan(fid, '%s', 'HeaderLines',1,'Delimiter',',');
fclose(fid);
celldisp(a)
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Xara
  • 8,748
  • 16
  • 52
  • 82

1 Answers1

8

There are several ways:

  1. Using cvsread:
    Assuming you have N rows in the file1:

    a = csvread( FILENAME, 0, 1, [0 1 N-1 1 ] );
    
  2. You might also consider xlsread

    a = xlsread( FILENAME, 'B:B' );  
    

    See specific example on the xlsread doc.

  3. Another option is dlmread

    a = dlmread( FILENAME, ',', [0 1 N-1 1] );
    

1 - A nice (and fast) way to count the number of lines in the file in Matlab can be found in this answer by Rody Oldenhuis.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • I just put N=10 and got this Warning: R and C should match RANGE(1:2). Use DLMREAD(FILE,DELIMITER,RANGE) instead. > In dlmread at 110 In csvread at 54 In read at 6 ??? Error using ==> dlmread at 145 Mismatch between file and format string. Trouble reading number from file (row 1, field 2) ==> DFA S Error in ==> csvread at 54 m=dlmread(filename, ',', r, c, rng); Error in ==> read at 6 a = csvread( FILENAME, 0, 0, [0 1 10 1 ] ); – Xara Jul 03 '13 at 07:18
  • ??? Undefined function or variable 'N'. Error in ==> read at 6 csvread( FILENAME, 0, 1, [0 1 N 1] ); – Xara Jul 03 '13 at 07:22
  • @Zara if you have an `"undefined ... 'N'"` error - this means that `N` is not defined. Haven't you set it to `10`??? – Shai Jul 03 '13 at 07:31
  • FILENAME = 'C:\Users\Desktop\Results.csv'; N=10; fid = fopen(FILENAME, 'rt'); a = csvread( FILENAME, 0, 0, [0 1 N 1 ] ); fclose(fid); celldisp(a) – Xara Jul 03 '13 at 07:36
  • I have used the above code and it gives me the error I have mentioned in forst comment – Xara Jul 03 '13 at 07:37
  • @Zara why `fopen` and `fclose`? you don't need to do this `cvsread` does it for you. – Shai Jul 03 '13 at 07:37
  • FILENAME = 'C:\Users\Desktop\Results.csv'; N=10; a = csvread( FILENAME, 0, 0, [0 1 N 1 ] ); celldisp(a) – Xara Jul 03 '13 at 07:42
  • Warning: R and C should match RANGE(1:2). Use DLMREAD(FILE,DELIMITER,RANGE) instead. > In dlmread at 110 In csvread at 54 In read at 3 ??? Error using ==> dlmread at 145 Mismatch between file and format string. Trouble reading number from file (row 1, field 2) ==> DFA S Error in ==> csvread at 54 m=dlmread(filename, ',', r, c, rng); Error in ==> read at 3 a = csvread( FILENAME, 0, 0, [0 1 N 1 ] ); – Xara Jul 03 '13 at 07:43
  • Error using ==> dlmread at 145 Mismatch between file and format string. Trouble reading number from file (row 1, field 2) ==> DFA S Error in ==> read at 3 a = dlmread( FILENAME, ',', [0 1 N-1 1] ); – Xara Jul 03 '13 at 07:49
  • I don't want to read the first row as it includes the headings. I want to start from 2nd row till end and read all the elements present in 2nd column. – Xara Jul 03 '13 at 07:50
  • 1
    @zara If you have a header row in the file, you might need to skip it: `a = dlmread( FILENAME, ',', [1 1 N-1 1] );` – Shai Jul 03 '13 at 07:51
  • @Zara please read the help section of the commands you are trying to run, so you can resolve these error by yourself. – Shai Jul 03 '13 at 07:52
  • How can you find `N` dynamically? - - I for instance notice that it changes dynamically for me. - - How can you remove empty cell tail in the end of the list? – Léo Léopold Hertz 준영 Nov 11 '16 at 05:08