Listing mat files in a directory and checking to see if they have a particular file.
이전 댓글 표시
Greetings I have been having a tough time creating a list of files (.mat files) in a directory in a listbox, and then checking to see if they contain a certain variable. I tried using uigetfile() but that hasn't panned out very well.
How do I get a list of files in a directory no matter what type they are and run a test on them to see if they contain a certain variable?
Thank you
Bill
채택된 답변
추가 답변 (2개)
Walter Roberson
2011년 10월 27일
hasvar = false;
try
s = whos('VariableYouWant', '-file', TheFileLocation);
if ~isempty(s); hasvar = true; end
catch
end
댓글 수: 2
K E
2012년 7월 20일
To search recursively for all files with a mat extension in order to subsequently apply Walter's solution, use getfilenames.m . Just mentioning this in case someone else wants to perform a recursive search for all mat files containing a desired variable.
Walter Roberson
2021년 3월 24일
A few years after the above comment from K E, MATLAB added the ability to search indefinite number of layers down:
dinfo = dir(fullfile(BaseFolder, '**', '*.mat'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
The folder field returned by dir() was added at the same time,
William
2011년 10월 27일
0 개 추천
댓글 수: 2
Fangjun Jiang
2011년 10월 27일
uigetdir()
Walter Roberson
2011년 10월 27일
uigetfile() if you want to choose which individual file to examine for the variable; uigetdir() if you want to choose which directory to examine. You would use uigetdir() together with dir() in order to examine all files in the chosen directory.
카테고리
도움말 센터 및 File Exchange에서 App Building에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!