Issue with for loop, finding file in directory

Hi,
I'm having a problem with the code below. dirNames is a 1x14 with the names of folders with csv files in them that I need to perform calculations on. The first for loop loops through the folders. The second for loop is to loop inside each folder to read the required files. I have left out the remaining code as that part it fine.
My issue is that S is 0 (see below), and I cannot get my head around the issue.
I would appreciate any help.
for a = 1 : length(dirNames)
fileDir = char(dirNames(a));
S = dir(fullfile(fileDir,'sheet*.csv')); % get list of data files in directory according to name structure
S = natsortfiles(S);
% Loop inside folder
for k = 1:length(S) % read data in specified sheet

답변 (1개)

Voss
Voss 2022년 6월 4일
편집: Voss 2022년 6월 4일
You mention the file "data_1" in a comment here:
which makes me think possibly your files are called data_1, data_2, etc., rather than sheet*whatever.csv. If that's true, then try using data_*.csv in fullfile instead of sheet*.csv to get the set of file names to read:
for a = 1 : length(dirNames)
fileDir = char(dirNames(a));
S = dir(fullfile(fileDir,'data_*.csv'));
S = natsortfiles(S);
% Loop inside folder
for k = 1:length(S) % read data in specified sheet
On the other hand, if the files are called file1.csv, file2.csv, etc., as is stated in the question itself:
Then you should use file*.csv in fullfile:
for a = 1 : length(dirNames)
fileDir = char(dirNames(a));
S = dir(fullfile(fileDir,'file*.csv'));
S = natsortfiles(S);
% Loop inside folder
for k = 1:length(S) % read data in specified sheet
The point is, you have to give fullfile the wild-card pattern that actually matches your file names.
By the way, S is not 0; it is an empty struct array (of size 0-by-1).
By the way again, you can use curly braces { } to index dirNames:
fileDir = dirNames{a};
and avoid the explicit call to char, regardless of whether dirNames is a cell array of character vectors a string array.

댓글 수: 4

I have tired. I don’t think it’s a problem with the name of time files and I have changed to dirNames{a} however the second for loop still won't run.
Check that fileDir exists and contains the files.
Then put a breakpoint at the line
S = dir(fullfile(fileDir,'file*.csv')); % or whatever wild-card pattern is accurate
and check the same thing while the program is running. That is, check isfolder(fileDir), and check isfile(fullfile(fileDir,'file1.csv')) or whatever name of a file that you know exists in fileDir.
If all looks ok but S is still empty, post a screen shot of a directory with a set of files in it, where we can see the names of the folder and files, and post the value of your variable fileDir and/or dirNames
filedir contains the name of one of the folders, no actual .csv files.
I have set my path to the folder where the 50+ folders are located and within these 50+ folders is where the .csv files are
Voss
Voss 2022년 6월 4일
I should have said, "Check that the directory whose name is given by the value of fileDir exists, and that this directory contains the files."
What did you find when you set the breakpoint in the code and did the checks I suggested?

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

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 6월 4일

댓글:

2022년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by