how to use dir in subfolders?

8 ビュー (過去 30 日間)
chlor thanks
chlor thanks 2016 年 6 月 29 日
コメント済み: Image Analyst 2019 年 4 月 21 日
I am trying to search for all the xlsx files inside a folder that have many subfolders to search from. I realize matlab does not search inside subfolders. So I tried this:
>> addpath(genpath('D:/'))
>> savepath
>> dir *.xlsx
but it is not working, please advice on what I can do. I want to make a code that works for any folder that I put inside genpath(' '). Thanks!
  1 件のコメント
Karan Gill
Karan Gill 2016 年 6 月 29 日
編集済み: Karan Gill 2016 年 6 月 29 日

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2016 年 6 月 29 日
See the attached demo.
  6 件のコメント
chlor thanks
chlor thanks 2016 年 7 月 1 日
It works! :D I can't tell you how much I appreciate all of your help, thank you so so so much!!
Image Analyst
Image Analyst 2019 年 4 月 21 日
Attached is a new script that works with the ** capability added in R2016b, which was released after our discussion above.
If you have R2016b or later, you should use this rather than the first script I gave in my Answer.

サインインしてコメントする。

その他の回答 (1 件)

Ba Mo
Ba Mo 2019 年 4 月 21 日
i dont recommend adding folders to matlab's path, casually like this
as lots of users reported above, new versions of matlab support the following command dir('**/*.mat');
However, old versions of matlab don't support this
instead of writing a large code, inspect the structure field "isfield" and so on, you could just easily ask DOS (or the command prompt) to do it for you. the output from MS-DOS isn't formatted, so you need to split the one block string to separate lines
newline = char(10); %char(10) is the character for line-break, or "enter"
[~,all_mats] = system('dir /s /b *.xls'); %you can also simply write: !dir /s /b *.xls
all_mats = strsplit(all_mats,newline)';
all_mats(cellfun(@isempty,all_mats))=[]; %the last entry/line might be an empty cell. delete it.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by