Read multiple CSV files from different folders uing readtable

조회 수: 27 (최근 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 CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by