필터 지우기
필터 지우기

How to read all CSV files from specific folders?

조회 수: 209 (최근 30일)
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 1일
댓글: Behtom 2023년 11월 9일
Hi, I want to open all CSV files and do a calculation on them. I used the code below and it worked
files = subdir('C:\Users\roozm\Desktop\New folder\*.csv');
Subdir function can do it easily. Now I want to be more specific and only open folder with the name of BIN and then read CSV files in only BIN folders.
How can I do that?
  댓글 수: 11
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 1일
I am just entering it in the command window. Honestly, I do not know how to check if the file is in my current directory.
Paolo
Paolo 2018년 6월 1일
I'm not sure I understand what you mean by 'I do not know how to check if the file is in my current directory.'
Lets say your current working directory is :
C:\Users\roozm\
as from your OP. Could you list the full path of the .csv files you want, this way we can build the dir command?

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

답변 (3개)

Walter Roberson
Walter Roberson 2018년 6월 1일
files = dir('C:\Users\roozm\Desktop\New folder\**\BIN*\*.csv');
fullpaths = fullfile({files.folder}, {files.name});
Now fullpaths is a cell array of fully qualified .csv files that are directly under a BIN* folder anywhere under "New folder"
  댓글 수: 10
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 4일
편집: Roozbeh Yousefnejad 2018년 6월 4일
I am afraid that there is another reason behind it as I am running the code in the debugging mode and step by step (I've attached a pic) So I believe xlsread() doesn't have a problem with the loop. Also, the files are closed.
As you can see in the picture, the curser is on the subset line and it is only the first xlsread command that I get error.
Behtom
Behtom 2023년 11월 9일
Thank You !

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


Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 1일
I mean, I don't know how to find my directory path. The file that I want to open is in this path
C:\Users\roozm\Desktop\New folder\F21802010055\BIN\CSV
which is on my desktop.
  댓글 수: 4
Paolo
Paolo 2018년 6월 1일
편집: Paolo 2018년 6월 1일
I was referring to this command:
dir('*/BIN*/*.csv')
Use this one if you are running from 'New folder'
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 1일
same error ...

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


Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 4일
I replaced filename = filepaths{i}; instead of filename=files(i).name but got this error
Undefined variable "filepaths" or class "filepaths".
Error in new (line 8)
filename = filepaths{i};
  댓글 수: 1
Roozbeh Yousefnejad
Roozbeh Yousefnejad 2018년 6월 4일
is filepaths a function? should I download it and add it to my code?

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

카테고리

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