Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Dic. de 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{:});
  1 comentario
Anirban Mandal
Anirban Mandal el 24 de Dic. de 2021
Editada: Anirban Mandal el 24 de Dic. de 2021
I tried cell2mat now to convert the cell into an array. It works fine also.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by