How to read Multiple CSV files in multiple sub folders
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mekala balaji
el 28 de Abr. de 2016
Respondida: Meade
el 2 de Mzo. de 2017
Hi,
I want to read multiple csv files in a subfolder(s). I have a main folder, in this main folder many sub folders (here two). I want to read each sub folder, and merge all csv in that subfolder. The merged CSV file name should be the respective subfolder name.
I use the following code, but I am unable to proceed after that, I do not know how to read each subfolder and merge the CVS files.
After merged, I want to read each CSV from output folder, and extract the data in column E (Score).
Kindly some one help.
clc;
clear all;
close all;
inputFolder='D:\Mekala_Backupdata\Matlab2010\InputData';
outputFolder='D:\Mekala_Backupdata\Matlab2010\OutputData';
expList=dir(inputFolder);
isub=[expList(:).isdir];
expID={expList(isub).name};
0 comentarios
Respuesta aceptada
Image Analyst
el 28 de Abr. de 2016
Editada: Image Analyst
el 28 de Abr. de 2016
You need to use genpath() to generate a list of all subfolders. Then you can use fullfile() and dir() to get a list of csv files in each one. See attached demo.
0 comentarios
Más respuestas (2)
J. Webster
el 28 de Abr. de 2016
Not sure exactly how you want to merge the csvs, but the basic way to run through all the directories is this...
inputFolder='D:\Mekala_Backupdata\Matlab2010\InputData';
cd(inputFolder);
directories = getDirs()
for i=1:length(directories);
mergeFilename = strcat(directory(i).name,'.csv'); %new filename is subfolder name with csv extention.
cd(directory(i).name);
files = dir('*.csv'); %you want just .csv
for j=1:length(files)
...merge your csv files here...
end
cd ..; % or cd(inputFolder);
end
0 comentarios
Meade
el 2 de Mzo. de 2017
This may get you started:
mainFolder = uigetdir(); % Selectyour Main folder
[~,message,~] = fileattrib([mainFolder,'\*']);
fprintf('\n There are %i total files & folders.\n',numel(message));
allExts = cellfun(@(s) s(end-2:end), {message.Name},'uni',0); % Get exts
CSVidx = ismember(allExts,'csv'); % Search ext for "CSV" at the end
CSV_filepaths = {message(CSVidx).Name}; % Use CSVidx to list all paths.
fprintf('There are %i files with *.CSV exts.\n',numel(CSV_filepaths));
for ii = 1:numel(CSV_filepaths)
file(ii)= importdata(CSV_filepaths{ii}) % Your parsing will be different
end
0 comentarios
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!