Read multiple CSV files from different folders uing readtable

22 ビュー (過去 30 日間)
root
root 2022 年 9 月 26 日
コメント済み: Image Analyst 2022 年 9 月 27 日
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
  2 件のコメント
KSSV
KSSV 2022 年 9 月 26 日
Check the paths......
Stephen23
Stephen23 2022 年 9 月 26 日
"I am getting only the last iteration"
If you want to store the data from every iteration then you will need to allocate that data to an array, just as the MATLAB documentaton shows:

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


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:
  1 件のコメント
Image Analyst
Image Analyst 2022 年 9 月 27 日
Then can you please click the "Accept this answer" link? Thanks in advance. 😃

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

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by