isdir does not work

12 views (last 30 days)
saxocat
saxocat on 28 Feb 2017
Commented: dpb on 1 Mar 2017
Hello yesterday i made a scipt that could search for specific filetypes in sub dictories and it was working fine.
fileList= dir('C:\Users\xxx\xxx')
fileList= fileList(~[fileList.isdir]);
txtFiles = dir(fullfile(fileList, '*.txt'));
But when i restarted i my computer the script didn't work. I have tried debug it and the problem seems to be that
fileList= fileList(~[fileList.isdir]);
wont list any files in the subfolders (it returns nothing). And it's like it just stopped working when on turned on my computer this morning.
  1 Comment
dpb
dpb on 28 Feb 2017
Since we can't see the content of the subject directory we can't tell much...the first (via crystal ball) guess is that you're not looking in the same subdirectory you were in yesterday. The script above looks ok on first blush; what is result for fileList?

Sign in to comment.

Accepted Answer

Jan
Jan on 28 Feb 2017
Edited: Jan on 28 Feb 2017
Don't panik. Matlab does not magically stop to work.
Use the debugger to examine, what's going on:
fileList = dir('C:\Users\xxx\xxx')
What is the contents of fileList? Is it empty? If so, this must be empty also:
fileList = fileList(~[fileList.isdir])
This "wont list any files in the subfolders"? Of course, because filList does not contain files in subfolders also. Or does "\xxx\" contain a "\**\"?
After this command fileList is still a struct array. Then:
dir(fullfile(fileList, '*.txt'))
fails with an error message: fullfile needs a string or cell string as input, not a struct array.
I guess, that you run another function then you assume. Please set a breakpoint in the code and check, if it is reached at all.
  6 Comments
Jan
Jan on 1 Mar 2017
@Image Analyst: I'm not sure if I like the new recursive dir(). For many years I thought about adding my own implementation to the FEX, preferrably as C-Mex. The world was waiting for this. But now... At least I have the chance to publish another progressbar. ;-)
dpb
dpb on 1 Mar 2017
I can't run the release here (no 32-bit version) so can't really try it out to see how it actually works in practice. I'd've thunk mimicking the OS command with a switch to request instead of mucking around with the file search string would've been the way to implement it, though.
I read the doc and didn't see another complaint I've lodged and requested as enhancement that can't see why wasn't implemented from the git-go is the ? wildcard character besides the *.
One can regexp after the fact, but often it's doable for a desired match straight from the search if had the ability to write the equivalent of the OS command. I never could understand why that wasn't just done to begin with.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!