Read multiple CSV files from different folders uing readtable

9 次查看(过去 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
  1 个评论
Image Analyst
Image Analyst 2022-9-26
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.

请先登录,再进行评论。

更多回答(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

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:

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by