필터 지우기
필터 지우기

opening different folders with different name and read CSV files

조회 수: 18 (최근 30일)
Hi, I want to open different folders with Matlab and then open different CSV files in each folder and read some specific sheets and columns. I can read the files from specific columns or sheet; however, my problem is that I cannot open different folders.
  • The first problem I have is that the files' name are in this form: F21802010055 or F21802010058 so they are not in row.
  • Also, each folder may have a different number of CSV files. For example folder F21802010055 has two CSV files with the names of F21802010055-201801-21_00-17 and F21802010055-201801-22_00-12 and folder F21802010058 has one csv file with the name of *F21802010058-201801-21_00-17*Can you please advise what would be the best approach to open these files?

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 28일
Use FEX::subdir to find all the files in the subfolders. Download this package, extract it and place it on MATLAB path. Here is a general template of what to do
files = subdir('C:\path_to_csv_folders\*.csv'); % here specify the path to top folder which contain all the other csv files
for i=1:numel(files) . % for loop will iterate over all file names
filename = files(i).name;
data = csvread(filename); % or readtable() or whatever function you are using to read csv file
% do you processing
end
Note that in subdir() all the CSV files doe not need to be present in the top folder. You can have several subfolders under the top folders and subfolder can contain several CSV files. subdir() can handle all the recursive file paths.
  댓글 수: 4
Ameer Hamza
Ameer Hamza 2018년 5월 28일
Alternatively you can use, set path button on the top bar in MATLAB window to add a folder to MATLAB path.
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 5월 28일
for example the csv file is F21802010055-201801-21_00-17.csv I also want to cut F21802010055-201801-21_00-17 and put it in front of the average numbers to know, which row is for which excel file.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 5월 28일
Thank you so much. It works perfectly. Can you just add, how I can have the excel number separately? currently, this is my code
clc
clear
files = subdir('C:\Users\roozm\Desktop\New folder\*.csv'); % here specify the path to top folder which contain all the other csv files
N=zeros(numel(files),9);
for i=1:numel(files) % for loop will iterate over all file names
filename = files(i).name;
sheet = 1;
subset = xlsread(filename,sheet,'A:C');
subset2 = xlsread(filename,sheet,'F:G');
subset3 = xlsread(filename,sheet,'K:N');
subset_merged=[subset,subset2,subset3];
subset_tot=subset_merged([end-6:end-2],:);
M=mean(subset_tot); % or readtable() or whatever function you are using to read csv file
N(i,:)=M;
end
so actually I take an average and I have an average for all the excel files and put in in the matrix name N. Now, I want to have the number of the excel file to know which row is for which excel file. How can I do it?
  댓글 수: 2
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 5월 28일
for example the csv file is F21802010055-201801-21_00-17.csv I also want to cut F21802010055-201801-21_00-17 and put it in front of the average numbers to know, which row is for which excel file.
Ameer Hamza
Ameer Hamza 2018년 5월 29일
Yes. You can get this by skipping last 4 characters from the filename
filename(1:end-4)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by