How to read many csv files at once and then extract certain column from it?

1 view (last 30 days)
I have about 115 csv files with 9 columns and rows ranging from 10^6 to 10^7. I want to read all of them at once and extract the necessary columns from it later. I tried the following code below for combining them. But I am not being able to extract a certain column. Any suggestion?
clear
clc
file=dir('*.csv');
num=length(file);
combined=cell(length(file),1);
for i=1:num
combined{i}=xlsread(file(i).name);
end

Accepted Answer

Walter Roberson
Walter Roberson on 24 Dec 2021
file=dir('*.csv');
num=length(file);
combined = cell(num, 1);
for i=1:num
combined{i}=xlsread(file(i).name);
end
desired_column = 7;
certain_column_cell = cellfun(@(C) C(:,desired_column), combined, 'uniform', 0);
If all of the files have the same number of rows and you want to combine into one matrix,
certain_column_matrix = horzcat(certain_column_cell{:});

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!