Code covered by the BSD License  

Highlights from
Directory to Cell

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

Directory to Cell

by

Kevin Crosby (view profile)

 

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

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)

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)

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)

Jan,
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
Updates
10 Aug 2010

Implemented Jan Simon's suggestions to eliminate CELL2MAT.

Contact us