How to upload csv. files from multiple folders
5 ビュー (過去 30 日間)
古いコメントを表示
Dear Matlab users,
I want to automize my program by uploading automatic csv. files from different folders. For example:
I have 15 folders and each folder has 4 csv files. Currently in my program i am uploading for each cycle(for loop) 4 csv. files...and running and getting results similarly for second cycle again i am uploading from second folder 4 csv files and running and so on for 15 times. Can anybody help me ? Attached my codes
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));
Total_Files = 1:numel(CSV_filepaths);
for Folder=1:numel(Total_Files)/2
[filenames pathname]=uigetfile('*.CSV','MultiSelect', 'on');
if isa(filenames,'cell')
name_order=0;
ind_order=0;
for i=1:max(size(filenames))
name_order=[name_order str2num(filenames{i}(10:end-4))];
ind_order=[ind_order i];
end
name_order=[name_order(2:end);ind_order(2:end)];
name_order_sorted=sortrows(name_order')';
name_ind=0;
for i=name_order_sorted(2,:)
name_ind=name_ind+1;
filenames_sorted{name_ind}=filenames{i};
end
filenames=filenames_sorted;
for i=1:max(size(filenames))
data=importdata([pathname filenames{i}]);
data=data.data;
files(i).name=filenames{i};
files(i).data=data;
end
elseif isa(filenames,'char')
data=importdata([pathname filenames]);
data=data.data;
files.name=filenames;
files.data=data;
else
error('No files selected !');
end
for i= 1:max(size(files))
display(files(i).name)
data=files(i).data;
My_data_t(:,1)=data(:,1)
My_data(:,2)=data(:,2)
end
end
0 件のコメント
回答 (1 件)
prasanth s
2019 年 10 月 11 日
編集済み: prasanth s
2019 年 10 月 11 日
to automate the file and folder access operations, do not use 'uigetfile' or 'uigetdir' functions.
use 'dir' function to get the file and folder names of any directory.
D=dir('E:\folder');
to get all 'csv' filenames in 'myfolder'
F=dir('myfolder\*.csv')
loop through the output struct array 'F' and get the filenames
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!