Search for all mat datafiles in directory
13 次查看(过去 30 天)
显示 更早的评论
I want to return a cell array containing absolute file path/locations of all .mat datafiles given an initial folder location. It should search all subfolders etc. Not sure how to implement this, and if it should be done recursively...?
Any ideas?
回答(1 个)
Stephen23
2017-7-19
Something like this perhaps:
D = the root directory;
S = dir(fullfile(D,'**','*.mat'));
N = {S.name};
F = {S.folder};
2 个评论
Stephen23
2017-7-19
编辑:Stephen23
2017-7-19
@Seb: Your code has "features" that should be fixed:
- char is confusing and totally superfluous as the names are already character.
- using fullfile is much better than hardcoding with file separators.
- using fullfile is much better than using strcat.
- using size is confusing: you should simply use numel.
- self is not used anywhere, and implies that you are trying to write a MATLAB function as it were a Python class. What is self?
- you should always load into a structure:
S = load(...);
- variables in this function's workspace are local to that workspace. If you want the data to be available in another workspace (e.g. the base workspace) then you will need to pass it as an output argument. MATLAB variables do not magically jump between workspaces:
- the code would be improved by following the examples shown in the documentation:
- if you want a truly recursive dir then have a look on FEX:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!