read all csv files in a direcrory in matlab

You can use a combination of dir function to get a list of files in a directory, and a for loop to read each CSV file using the readtable function. Here's an example code snippet:

main.m
% Define the directory where CSV files are stored
dir_csv = '/path/to/csv/files/';

% Get list of CSV files in directory
all_files = dir(fullfile(dir_csv, '*.csv'));

% Loop through each CSV file and read its data
for i = 1:length(all_files)
    filename = fullfile(dir_csv, all_files(i).name);
    data{i} = readtable(filename);
end
334 chars
12 lines

This code creates a data cell array where each element corresponds to a CSV file in the directory. You can access each table using curly braces {} and the index of the table in the cell array. For example, data{1} will give you the first CSV file's data.

related categories

gistlibby LogSnag