Documentation Center |
List folder contents
dir
dir name
listing = dir(name)
dir lists the files and folders in the MATLAB® current folder. Results appear in the order returned by the operating system.
dir name lists the files and folders that match the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. You can use wildcards (*).
View the Contents of a Folder
View the contents of the matlab/audiovideo folder:
dir(fullfile(matlabroot, 'toolbox/matlab/audiovideo'))
Find Information in the Return Structure
Return the folder listing, restricted to files with a .m extension, to the variable av_files:
av_files = dir(fullfile(matlabroot, ...
'toolbox/matlab/audiovideo/*.m'))MATLAB returns the information in a structure array:
av_files =
25x1 struct array with fields:
name
date
bytes
isdir
datenumIndex into the structure to access a particular item:
av_files(3).name ans = audioplayerreg.m
– Use the Wildcard Character to Find Multiple Files
View the MAT-files in the current folder that include the term my_data by using the wildcard character:
dir *my_data*.mat
MATLAB returns all file names that match this specification. For instance, it returns the following if they are in the current folder:
old_my_data.mat my_data_final.mat my_data_test.mat
– Exclude Certain Files from the Output
Return the list of files in the current folder, excluding those files that dir cannot query:
y = dir;
y = y(find(~cellfun(@isempty,{y(:).date}))); – Find the Date a File Was Modified
To get the serial date number for the date and time a file was last modified, use the datenum field of the structure returned by the dir command:
DirInfo = dir('startup.m');
filedate = DirInfo.datenumUsing the datenum function to convert the string returned in the date field of the structure is not recommended as this can behave differently in different locales.
filedate = datenum(DirInfo.date)
cd | fileattrib | isdir | ls | mkdir | rmdir | what
[1] UNIX is a registered trademark of The Open Group in the United States and other countries.