Code covered by the BSD License  

Highlights from
Directory to Cell

4.33333

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

Directory to Cell

by

 

10 Aug 2010 (Updated )

Converts directory listing into a cell array.

| Watch this File

File Information
Description

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.
 
Example:
 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

Nice! Thank you!

18 Mar 2014 Norberto

Very useful! Thanks

18 Mar 2014 Norberto  
11 Aug 2010 Kevin Crosby

Zach,

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)

11 Aug 2010 Zachary Danziger

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

10 Aug 2010 Kevin Crosby

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

10 Aug 2010 Jan Simon

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

Works with MATLAB 7.10 (R2010a)

Updates
10 Aug 2010

Implemented Jan Simon's suggestions to eliminate CELL2MAT.

Contact us