Code covered by the BSD License  

Highlights from
get_contents

Be the first to rate this file! 14 Downloads (last 30 days) File Size: 2.95 KB File ID: #30835

get_contents

by

 

23 Mar 2011 (Updated )

Get the contents of a specified directory

| Watch this File

File Information
Description

cont = get_contents(directory)
cont = get_contents(directory,options)
[cont,dirflag] = get_contents(...)

This function returns the contents of a specified directory.

cont = get_contents(directory) returns the files and folders in directory and returns them to the cell array cont. It ignores hidden files and folders (those starting '.'). Directory must be a character array (string).

cont = get_contents(directory,options) allow search options to be specified. The options are specified in parameter/value pairs, and include:
    'rec' {false} | true
                Search recursively within the subfolders of the specified directory.
    'path' {'relative'} | 'full'
                Specifies whether returned paths are full or relative to the specified directory.
    'sort' {false} | true
                Specify whether the output is sorted alphabetically.
    'filter' {'all'} | 'files' | 'folders' | '*.ext' | str
                This option allows a filter to be specified. 'files' returns names of all files in the directory. 'folders' returns names of all folders in the directory. '*.ext', where 'ext' is a user-specified file extension, returns all files with the extension '.ext'. str may be any string; only elements that contain str will be returned (files or folders). str is case-sensitive.

[cont,dirflag] = get_contents(...) returns a logical array dirflag, the same size as cont, indicating whether each element is a directory.

Examples

    Ex. 1

    % Return all m-files in the current directory

    cont = get_contents(cd,'filter','*.m')

    Ex. 2

    % Return all files in the current directory and its
    % sub-directories

    cont = get_contents(cd,'rec',true)

    Ex. 3

    % Return all files in current directory with names
    % containing 'foo'
    
    % may return files and folders:
    [cont,dirflag] = get_contents(cd,'filter','foo')

    % use dirflag to limit:
    cont = cont(~dirflag);

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Updates
28 Mar 2011

Updated help

10 Aug 2011

Added a keyword, allowing specific file extensions to be returned

11 Aug 2011

Some bug fixes. Additionally outputs a directory flag.

18 Jan 2012

Some bug fixes and additional inputs options permitted, including a recursive option. Input now in parameter/value pairs.

25 Apr 2013

Simplified interface such that str is not required (now varargin option). Also made sort and rec flags logical rather than strings.

03 Jun 2013

Minor update to help text and description.

03 Jun 2013

Minor update to file help text and description.

Contact us