Reading a specific column from a CSV file in Matlab

I am trying to read a CSV file in matlab. I just want to read the second column, but the code below outputs everything to a CSV file. What parameters or functions should I make so that it reads only the second column

FILENAME = 'C:\Users\Desktop\Results.csv'; fid = fopen(FILENAME, 'rt'); a = textscan(fid, '%s', 'HeaderLines',1,'Delimiter',','); fclose(fid); celldisp(a) 
+6
source share
1 answer

There are several ways:

  • Using cvsread :
    Assuming file 1 has lines N :

     a = csvread( FILENAME, 0, 1, [0 1 N-1 1 ] ); 
  • You may also consider xlsread

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

    See a specific example in the xlsread document.

  • Another option is dlmread

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

1 - A good (and quick) way to count the number of lines in a file in Matlab can be found in this answer from Rody Oldenhuis .

+8
source

Source: https://habr.com/ru/post/948605/


All Articles