Changing file path and load multiple files
조회 수: 15 (최근 30일)
이전 댓글 표시
I have some folders where i have saved several files where code changes and I need to load each file. I need help
댓글 수: 0
채택된 답변
Image Analyst
2023년 2월 26일
Use a file data store fileDatastore
% Specify the top level folder.
topLevelFolder = pwd; % Wherever it is....
% Process a sequence of files.
filePattern = fullfile(topLevelFolder, '*.xls*');
fds = fileDatastore(filePattern, 'ReadFcn',@readtable) % Create an image Datastore
% Get all filenames into one cell array. Filenames have the complete path (folder prepended).
allFileNames = fds.Files;
numFiles = numel(fds.Files);
for k = 1 : numel(allFileNames)
% Get this file name.
fullFileName = allFileNames{k};
fprintf('Processing %s\n', fullFileName);
% Now do something with fullFileName, such as passing it to readtable().
end
댓글 수: 2
Image Analyst
2023년 2월 27일
편집: Image Analyst
2023년 2월 27일
If you don't want the order the operating system gives you, then you can have for loops over code and year and build up the filename with sprintf().
Image Analyst
2023년 3월 1일
Yes, you just load the workbook file into the "data" variable and then you never do anything with data. Plus on every iteration data is overwritten by the current workbook so at the very end you'll only have data from the very last workbook. If you want to do something with data, then do so. If you also want to save it for use later, outside the loop, then you'll need to save it somehow, like in a cell array.
data{k} = readtable(fullFileName);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!