File Exchange

image thumbnail

Directory to Cell

version 1.1 (1.68 KB) by

Converts directory listing into a cell array.

4.33333
4 Ratings

4 Downloads

Updated

View License

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')

Comments and Ratings (8)

Nathan

Nathan (view profile)

Nice! Thank you!

Norberto

Very useful! Thanks

Norberto

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)

Zachary Danziger

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

Kevin Crosby

Kevin Crosby (view profile)

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

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]';

Kevin Crosby

Kevin Crosby (view profile)

Works with MATLAB 7.10 (R2010a)

Updates

1.1

Implemented Jan Simon's suggestions to eliminate CELL2MAT.

MATLAB Release
MATLAB 7.10 (R2010a)

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

» Watch video