load all files of a certain type in a folder in matlab

You can use the readdir function to get a list of all files in a folder, and then filter the list based on their file extension. Here's an example function that does this:

main.m
function file_list = load_files(folder_path, extension)
% Load all files with a given extension in a folder
%
% Inputs:
%   folder_path: the path to the folder to search
%   extension: the file extension to filter by (e.g. '.csv', '.txt')
%
% Outputs:
%   file_list: a cell array of file names with the given extension

% Get a list of all files in the folder
dir_list = dir(folder_path);

% Filter the list to only include files with the given extension
file_list = {dir_list(~[dir_list.isdir]).name};
file_list = file_list(~cellfun('isempty',regexp(file_list, extension)));

% Add the folder path to the file names
file_list = cellfun(@(x) fullfile(folder_path, x), file_list, 'UniformOutput', false);
end
708 chars
21 lines

You can call this function with the path to your folder and the file extension you want to load, like this:

main.m
folder_path = '/path/to/folder';
extension = '.csv';
file_list = load_files(folder_path, extension);
101 chars
4 lines

This will return a cell array of the file names with the '.csv' extension in the given folder.

gistlibby LogSnag