Read multiple CSV files from different folders uing readtable

44 ビュー (過去 30 日間)
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

採用された回答

Image Analyst
Image Analyst 2022 年 9 月 26 日
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
  3 件のコメント
Bizuayehu Addisie Beyene
Bizuayehu Addisie Beyene 2022 年 9 月 26 日
I can see its printed on the command window... I am doing this and i have the follwoing error: K is 1 for this case...which was supposed to be length of files.... I am confused here ....
Thank you so much again
myFolder = p3;%'C:\Users\yourUserName\Documents\My Pictures';
% Get a list of all files in the folder with the desired file name pattern.
file= fullfile(myFolder, '*.CSV'); % Change to whatever pattern you need.
Files = dir(file);
B=zeros(198,1);% length(L(:,3)
for k = 1 : length(Files)
FileName = Files(k).name;
fullFileName = fullfile(Files(k).folder, FileName);
l = readtable(fullFileName);
B(:,k)=l{:,3}
end
histogram(B)
error: Unable to perform assignment because the
size of the left side is 198-by-1 and the
size of the right side is 6-by-1.
Error in seasonal_bubble_2014 (line 38)
B(:,k)=l{:,3}

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2022 年 9 月 26 日
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
  6 件のコメント
Bizuayehu Addisie Beyene
Bizuayehu Addisie Beyene 2022 年 9 月 26 日
I want to store the values of f as shown below to make histogram. this way i am getting only the last iteration.
Any help
thank you
myFolder = p3;%
% Get a list of all files in the folder with the desired file name pattern.
file= fullfile(myFolder, '*.CSV'); % Change to whatever pattern you need.
Files = dir(file);
for k = 1 : length(Files)
FileName = Files(k).name;
fullFileName = fullfile(Files(k).folder, FileName);
l = readtable(fullFileName);
f=l{:,3};
end
histogram(f,4)

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 9 月 27 日
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:
  3 件のコメント
Bizuayehu Addisie Beyene
Bizuayehu Addisie Beyene 2022 年 9 月 27 日
I already accepted your answer !
Havea good day :)

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by