how to load multiple matfiles from another folder?
12 views (last 30 days)
Show older comments
how to load multiple matfiles from another folder?
1 Comment
LeoAiE
on 30 Oct 2021
Hi,
There are many ways to load multiple files into MATLAB but first and foremost is to add the folder to you path. If the files have similar names you can use a wild card with datastore function https://www.mathworks.com/help/matlab/datastore.html A datastore allows you to read and process data stored in multiple files on a disk, a remote location, or a database as a single entity. If the data is too large to fit in memory, you can manage the incremental import of data, create a tall array to work with the data, or use the datastore as an input to mapreduce for further processing.
And example of how to load multiple files If your files names are file1, file2, files3 etc Then use something like Data = readtable(‘file*.mat’) If you find this answer helpful, consider accepting it! Thanks
Accepted Answer
Image Analyst
on 30 Oct 2021
See the FAQ:
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in with load()
s = load(fullFileName)
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!