Skip to Main Content Skip to Search
Product Documentation

dir - List folder contents

Syntax

dir
dir name
listing = dir(name)

Description

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 (*).

listing = dir(name) returns attributes about name.

Tips

Input Arguments

name

A string value specifying a file or folder name.

Output Arguments

listing

Field Name

Description

Class

name

File or folder name

char array

date

Modification date timestamp

char array

bytes

Size of the file in bytes

double

isdir

1 if name is a folder; 0 if not

logical

datenum

Modification date as serial date number. The value is locale-dependent.

double

Examples

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
    datenum

Index 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.datenum

Using 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)

Alternatives

Use the Current Folder browser to view the list of files in a folder.

See Also

cd | fileattrib | isdir | ls | mkdir | rmdir | what

How To


[1] UNIX is a registered trademark of The Open Group in the United States and other countries.

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS