How can I import multiple .csv files that are in separate folders

조회 수: 2 (최근 30일)
Cole Benson
Cole Benson 2017년 3월 2일
답변: Image Analyst 2017년 3월 2일
I am trying to import a large set of data and the problem I am running into is that they are stored in multiple folders inside a Main folder. I have changed my directory to this Main folder but it won't let me loop through the .csv files in the Main folder as they each are in their separate folder. Unfortunately, this is how the data is output by our data collection machines so is there a way I can potentially write a code that opens each folder and extracts the file information? I appreciate the help
Edit: I also do not have any sample code as this is the first step in my process

답변 (2개)

Meade
Meade 2017년 3월 2일
Try this:
!!Careful, this will find ALL csvs in a folder!! You can further narrow down, e.g. by filename, using additional logical indices
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)
% Do import here
end

Image Analyst
Image Analyst 2017년 3월 2일
See attached demos, one for releases prior to R2016b and one for R2016b and later.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by