View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Directory to Cell

4.3 | 4 ratings Rate this file 5 Downloads (last 30 days) File Size: 1.68 KB File ID: #28442 Version: 1.1

Directory to Cell


Kevin Crosby (view profile)


10 Aug 2010 (Updated )

Converts directory listing into a cell array.

| Watch this File

File Information

NAMES = DIR2CELL converts the directory listing into a cell array in alphabetical order.
NAMES = DIR2CELL(ARG) converts the directory listing based on argument ARG, into a cell array in alphabetical order. ARG may contain wildcards.

[NAMES, DATENUMS, DATES, BYTES, ISDIRS] = DIR2CELL(ARG), also returns the modification date as a MATLAB serial date number in double array DATENUMS, the modification date in cell array DATES, the number of bytes in double array BYTES, and a Boolean variable designating if is a directory or not in logical array ISDIRS.
 files = dir2cell('*.m')

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
04 Jun 2014 Nathan

Nathan (view profile)

Nice! Thank you!

18 Mar 2014 Norberto

Very useful! Thanks

18 Mar 2014 Norberto

11 Aug 2010 Kevin Crosby

Kevin Crosby (view profile)


I have tried updating my program with your suggestion (See below).

However, the timing for my test cases appears to be almost the same, with my original code being slightly faster.

if ~exist('arg', 'var') || isempty(arg)
d = struct2cell(dir)';
else % if exist('arg', 'var') && ~isempty(arg)
d = struct2cell(dir(arg))';
end % if ~exist('arg', 'var') || isempty(arg)

if isempty(d)
[names, dates] = deal({}); % make size equal 0
[datenums, bytes, isdirs] = deal([]);
else % if ~isempty(d)
%[names dates bytes isdirs datenums] = ... % a little slower
% deal(d(:,1), d(:,2), [d{:,3}]', [d{:,4}]', [d{:,5}]');
names = d(:,1);
dates = d(:,2);
bytes = [d{:,3}]';
isdirs = [d{:,4}]';
datenums = [d{:,5}]';
end % if isempty(d)

Comment only
11 Aug 2010 Zachary Danziger

You might also consider the fast alternative,
>> dirCell = struct2cell(dir)';

Comment only
10 Aug 2010 Kevin Crosby

Kevin Crosby (view profile)

Thanks for your comments. I'll upload an update based on your suggestion shortly.

Comment only
10 Aug 2010 Jan Simon

Jan Simon (view profile)

H1-Line, descriptive help section, example, date, version number, author with contact address, working, usable.
If you need the reply of DIR as cells and vectors instead of the standard struct format, this will be helpful.

You can create vectors without the slow CELL2MAT:
datenums = cell2mat({d.datenum}');
=> datenums = [d.datenum]';

10 Aug 2010 Kevin Crosby

Kevin Crosby (view profile)

Works with MATLAB 7.10 (R2010a)

Comment only
10 Aug 2010 1.1

Implemented Jan Simon's suggestions to eliminate CELL2MAT.

Contact us