user758077
user758077

Reputation:

importing data with specific columns (without headers) from csv files into MATLAB

Given a csv file, which looks like (from Import Tool in MATLAB)

enter image description here

one can import it to MATLAB by using the Import Tool. Suppose one has 30 such files with equal number of columns but possibly different number of rows.

Question: How can one achieve the following in MATLAB?

For each file, load columns 2, 3, 5 (all starting from row 2) as an cell array.


Lots of questions about importing data from csv files to MATLAB have been asked before. But answers to those related questions (the closest one I found is here) seem to be not very helpful to achieve my specific goal here.

Upvotes: 0

Views: 1347

Answers (2)

jkazan
jkazan

Reputation: 1199

This would be my suggestion:

% Load files with multiselect
[file,dir,filterIndex] =  uigetfile( ...
    {'*.csv', '*.csv';
    '*.*',  'All Files (*.*)'}, ...
    'Pick files',...
    'Multiselect', 'on');

% 'file' will be a cell array of file names if you read more than one file, but 
% it will be a char array if you only read one file. If only one file is
% chosen, put it into a cell.
if ischar(file)
    file = {file};
end

% Loop through all files and use textscan
n = length(file);
data = cell(n,1);
for k = 1:n
    fid = fopen([dir,file{k}]);
    data{k} = textscan(fid,'%*s %s %f %*s %s %*[^\n]',...
        'delimiter',',','HeaderLines',1);
    fclose(fid);
end

The textscan call above says:

  1. go through file fid
  2. Skip column 1
  3. Treat column 2 as a string
  4. Treat column 3 as a double
  5. Skip column 4
  6. Treat column 5 as a string
  7. Skip the rest of the columns
  8. Use comma delimiter
  9. Ignore first line

If you load n files, the variable data will be a nx30 cell array containing cell matrices with the data from each file.

I hope this help. Let me know if there is anything else I can do.

Upvotes: 1

Enrico Anderlini
Enrico Anderlini

Reputation: 457

Suppose you have the file text.csv which is as follows:

var1    var2    var3    var4
dummy   1   London  0.5
dummy   2   Edinburgh   1.5
dummy   3   Cardiff 2
dummy   4   Belfast 2.5

This may not be the most computationally efficient solution, but it is very effective (this should be copied to a Matlab script and run):

% Read the data as a table (without reading the column label, otherwise add
% 'ReadVariableNames',false before the right bracket:
T = readtable('text.csv','Delimiter',','); 
% Convert the table to a cell:
C = table2cell(T);
% Store, say, the 2nd and 3rd columns to another cell. Otherwise you can
% also convert numeric values to vectors/matrices:
C2 = C(:,2:3);

I hope this helps. Let me know if you have other questions. I would advise transforming the data to matrices if you need to perform operations.

Upvotes: 1

Related Questions