How to Apply Function Across Multiple Files With 'csvread'
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 15 de Mayo de 2017
Respondida: MathWorks Support Team
el 15 de Mayo de 2017
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
Respuesta aceptada
MathWorks Support Team
el 15 de Mayo de 2017
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Large Files and Big Data en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!