Listing mat files in a directory and checking to see if they have a particular file.

161 views (last 30 days)
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

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 27 Oct 2011
Files=dir(fullfile('MyFolder','*.mat')) will give you a list of .mat files.
Files=dir(fullfile('MyFolder','*.*')) will give you a list of all files and subfolders.
Files is a structure array, check its fieldnames.
Vars=load('MyData.mat') will return Vars as a structure. fieldnames(Vars) gives you all the variable names in the .mat file.
  1 Comment
Louis-Philippe Guinard
Louis-Philippe Guinard on 23 Mar 2021
Hello!
Waaaaay late to the party, but if by any chance someone needs this and finds the same error as me (I'm using 2018a BTW) I tried the first line that was suggested, I.E.:
Files = dir(fullfile(MyFolder, '*.mat'));
Fangjun has written the MyFolder between quotes (as in, 'MyFolder' ) but this returned me a 0x1 structure while it should have been a 30x1 structure in my case. The quotes made it so it searched for a folder named MyFolder, while I was already specifying a path prior to this. It then depends if MyFolder is a string name or a variable.
Hope this precision helps! :)

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 27 Oct 2011
hasvar = false;
try
s = whos('VariableYouWant', '-file', TheFileLocation);
if ~isempty(s); hasvar = true; end
catch
end
  2 Comments
K E
K E on 20 Jul 2012
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
Walter Roberson on 24 Mar 2021
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,

Sign in to comment.


William
William on 27 Oct 2011
You guys are Awesome. One last thing. How do I specify the file location from within the Gui. I would like the user to be able to choose. Can I just use UIgetfile()?
Thanks
  2 Comments
Walter Roberson
Walter Roberson on 27 Oct 2011
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.

Sign in to comment.

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!