필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Walter Roberson
Walter Roberson 2021년 12월 24일
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
Anirban Mandal
Anirban Mandal 2021년 12월 24일
편집: Anirban Mandal 2021년 12월 24일
I tried cell2mat now to convert the cell into an array. It works fine also.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by