How to Apply Function Across Multiple Files With 'csvread'

조회 수: 4 (최근 30일)
I have 8000 data files, and I need to take the average of the first column across each consecutive set of 8 files. This would yield a total of 1000 mean values in a 1000 x 1 array. The code I have written below calculates the mean values of column 1 in files 1-8. This yields 8 mean values, but I want just 1 mean value across all 8 files. I do not know how to modify this code so that it automatically calculates the mean values of column 1 for files 1-8, 9-16, 17-24, etc.
I have looked at examples on MATLAB’s website, but I was not able to find the answer to my question. Would you please show me how I may modify this code to accomplish my goal? 
directory='/desktop/';
files=dir([directory,'*.CSV']);
means=[];
for i=1:8
%Import data files
data=csvread([directory,files(i).name],1,0);
%Select first column
firstColumn=data(:,1);
%Calculate average of first column
means=[means mean(firstColumn)];
end

채택된 답변

MathWorks Support Team
MathWorks Support Team 2017년 5월 15일
You can accomplish this using the ‘datastore’ function. The function ‘csvread’ loads in the entire file into memory while you only need the first column. On the other hand, ‘datastore’ can read in only parts of the file and is essentially a pointer to the file. This will yield your code to run quicker.
Attached you will find an example using ‘datastore’ with comments. You can refer to the documentation below for more information.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by