Read multiple CSV files from different folders uing readtable

29 views (last 30 days)
Hello
I have to read multiple csv files from differnt folders on the same editor window to plot on the same figure.
I have tried sth like this
ds1 = datastore('*.csv');
T = readall(ds1);
buit this gives data for only the currecnt directory.
I have file paths p1 , p2 and p3 for each 3 folders and i want to read thoese files from the 3 folder,
I am stuck and appreciate your help
Thank you

Accepted Answer

Image Analyst
Image Analyst on 26 Sep 2022
See the FAQ:
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My CSV Files';
% 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, and its subfolders, with the desired file name pattern.
filePattern = fullfile(myFolder, '**/*.csv'); % 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 as a table.
thisTable = readtable(fullFileName);
end
  1 Comment
Image Analyst
Image Analyst on 26 Sep 2022
Because you left the semicolon off, that should print out the values of B to the command window. Do you not see them?
If you want to store/save them for later, you'll have to make B a matrix and give the column number, like
B(:, k) = data(:, 3); % Store 3rd column of data in k'th column of B.

Sign in to comment.

More Answers (2)

KSSV
KSSV on 26 Sep 2022
thepaths = {p1,p2,p3} ;
for i = 1:3 % loop for ech folder
csvFiles = dir([thepaths{i},'\*csv']) ; % get ll csv files in the folder
N = length(csvFiles) ;
for j = 1:N % loop for each csv file
csvFile = fullfile(csvFiles(j).folder,csvFiles(j).name) ;
T = readtable(csvFile) ;
% do what you want
end
end

Image Analyst
Image Analyst on 27 Sep 2022
You need to index f. Actually I'd use more descriptive variable names. No one wants to look at code that looks like aphabet soup.
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.CSV'); % Change to whatever pattern you need.
fileList = dir(filePattern);
for k = 1 : length(fileList)
% Read in this table.
baseFileName = fileList(k).name;
fullFileName = fullfile(fileList(k).folder, baseFileName);
thisTable = readtable(fullFileName);
% Extract the third column only.
column3 = thisTable{:, 3};
% Append this column to our master vector.
if k == 1
allColumn3s = column3;
else
allColumn3s = [allColumn3s; column3]
end
end
numBins = 4;
histogram(allColumn3s, numBins)
grid on;
If you have any more questions, then attach your data (at least 2 CSV files) with the paperclip icon after you read this:

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!