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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by