| MATLAB® | ![]() |
As an alternative to the dir function, you can use the Current Directory browser to view directory contents.
dir
dir name
files = dir('dirname')
dir lists the files in the current working directory. Results are not sorted, but presented in the order returned by the operating system.
dir name lists the specified files. The name argument can be a path or file name, or can include both. You can use absolute and relative path names and wildcards (*).
files = dir('dirname') returns the list of files in the specified directory (or the current directory, if dirname is not specified) to an m-by-1 structure with the fields listed here.
Field Name | Description | Data Type |
|---|---|---|
| name | File name | char array |
| date | Modification date timestamp | char array |
| bytes | Number of bytes allocated to the file | double |
| isdir | 1 if name is a directory; 0 if not | logical |
| datenum | Modification date as serial date number | double |
On Microsoft Windows platforms, you can obtain a list of available drives using the DOS net use command. In the Command Window, run
dos('net use')Or run
[s,r] = dos('net use')to return the results to the character array r.
The MATLAB dir function is consistent with the Microsoft Windows OS dir command in that both support short file names generated by DOS. For example, both of the following commands are equivalent in both 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
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 and the symbolic link points to a nonexistent target. A nonexistent target is when a target has been moved, removed, or renamed. For example, if my_file in my_dir is a symbolic link to another file that has been 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 Files That Cannot Be Queried
To view the contents of the matlab/audiovideo directory, type
dir(fullfile(matlabroot, 'toolbox/matlab/audiovideo'))
To view the MAT-files in your current working directory that include the term java, type
dir *java*.mat
MATLAB returns all file names that match this specification:
java_array.mat javafrmobj.mat testjava.mat
To view the M-files in the MATLAB audiovideo directory, type
dir(fullfile(matlabroot,'toolbox/matlab/audiovideo/*.m'))
MATLAB returns
Contents.m aviinfo.m render_uimgraudiotoolbar.m audiodevinfo.m aviread.m sound.m audioplayerreg.m lin2mu.m soundsc.m audiorecorderreg.m mmcompinfo.m wavfinfo.m audiouniquename.m mmfileinfo.m wavplay.m aufinfo.m movie2avi.m wavread.m auread.m mu2lin.m wavrecord.m auwrite.m prefspanel.m wavwrite.m avifinfo.m render_fullaudiotoolbar.m
To return the list of files to the variable av_files, type
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
datenumIndex into the structure to access a particular item. For example:
av_files(3).name ans = audioplayerreg.m
To return the list of files excluding those that cannot be queried, run the following:
y = dir;
y = y(find(~cellfun(@isempty,{y(:).date}))); cd, copyfile, delete, fileattrib, filebrowser, fileparts, genpath, isdir, ls, matlabroot, mkdir, mfilename, movefile, rmdir, type, what
Managing Files and Working with the Current Directory
[1] UNIX is a registered trademark of The Open Group in the United States and other countries.
![]() | diffuse | dir (ftp) | ![]() |
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |