Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

dir - Folder listing

GUI Alternatives

As an alternative to the dir function, use the Current Folder browser.

Syntax

dir
dir(name)
listing = dir('name')
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 specified by the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. name can include wildcards (*).

listing = dir('name') returns attributes about name to an m-by-1 structure, where m is the number of files or folders in name. The following table shows the fields in the structure.

dir name is the command syntax.

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

Remarks

Listing Drives

On Microsoft Windows platforms, to obtain a list of available drives, use the DOS net use command. In the Command Window, run

dos('net use')

Or run

[s,r] = dos('net use')

MATLAB returns the results to the character array r.

DOS File Names

The MATLAB dir function is consistent with the Microsoft Windows operating system dir command in that both support short file names generated by DOS. For example, both of the following commands are equivalent in Windows and MATLAB:

dir long_matlab_mfile_name.m
   long_matlab_mfile_name.m  

dir long_m~1.m
   long_matlab_m_file_name.m 

Structure Results for Nonexistent Files

When you run dir with an output argument and the results include a nonexistent file or a file that dir cannot query for some other reason, dir returns the following default values:

		date: '' 
		bytes: [] 
		isdir: 0 
		datenum: [] 

The most common occurrence is on UNIX[1] platforms when dir queries a file that is a symbolic link, which points to a nonexistent target. A nonexistent target is a target that was moved, removed, or renamed. For example, if my_file in my_dir is a symbolic link to another file that was deleted, then running

r = dir('my_dir')

includes this result for my_file:

r(n) = 
		name: 'my_file'
		date: ''
		bytes: []
		isdir: 0
		datenum: []

where n is the index for my_file, found by searching r by the name field. See also the example Excluding Results That Cannot Be Queried

Examples

Listing the Contents of a Specified Folder

View the contents of the matlab/audiovideo folder:

dir(fullfile(matlabroot, 'toolbox/matlab/audiovideo'))

Returning List to Structure

Return the list of results to the variable av_files:

av_files = dir(fullfile(matlabroot, ...
               'toolbox/matlab/audiovideo/*.m'))

MATLAB returns the information in a structure array:

av_files = 
24x1 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

Using Wildcard and File Extension

View the MAT-files in the current folder that include the term my_data:

dir *my_data*.mat

MATLAB returns all file names that match this specification:

old_my_data.mat  my_data_final.mat  my_data_test.mat

Excluding Results That Cannot Be Queried

Return the list of files excluding those files that cannot be queried:

 y = dir; 
 y = y(find(~cellfun(@isempty,{y(:).date}))); 

See Also

cd, fileattrib, isdir, ls, mkdir, rmdir, what

Topics in User Guide:


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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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