Code covered by the BSD License  

Highlights from
Mosaic generator

image thumbnail

Mosaic generator

by

 

15 Jan 2011 (Updated )

Photo mosaic generator. Choose a tile image, and a directory with images, and see the magic.

fuf (folders, isRecurcive, opt, isIgnoreIllegalFolders)
function srtlist = fuf (folders, isRecurcive, opt, isIgnoreIllegalFolders)
%FUF recursively retrieves files under specified folder(s).
%   
%   Syntax: srtlist = fuf (folders,isRecurcive,opt)
%
%   INPUT ARGUMENTS
%       - folders       can be a character array or a cell array of strings. Valid strings
%                       are matlab path;  
%       - isRecurcive        {1} optional array  of  logicals: true stands for recursive
%                       search, whilst false limit the search to the specified folder.
%                       isRecurcive can be either a single logical, or an array of same
%                       dimentions as folders array.
%                       Use the 2nd option to specify the search rule on a folder-basis.
%                       Default is 1, iFolder.filePath., each folder is searched for recursively.
%       - opt           {'normal'} | 'detail'  is an optional field: when it's set to 'detail',
%                       FUF returns the full path of files searched for, while, when set to
%                       normal, it returns file names only.
%
%
%   OUTPUT ARGUMENT
%       - srtlist   is a sorted cell array of strings containing the name or the full path
%                       of files recursively found under the given folders.
%
%   FUF applies the wildcard * for searching files through all the directories beneath the given folder(s)
%   if the isRecurcive parameter is set to 1. If you want to have different wildcards for folders belonging
%   to the same tree root, you need to pass them separately as input arguments.
%
%   REMARKS
%       - Folders don't have to belong to the same directory, therefore this function doesn't
%         have to be invoked from a particular directory.
%       - Matlab partial paths are not valid input arguments.
%       - Wildcard * can be used to narrow the search.
%       - To get the full path of a given file, FUF doesn't use the which command,
%         therefore it normally works also with java function that are not loaded.
%       - After completion, the working directory is set to the current directory at the time
%         of the function call.
%
%   EXAMPLES
%
%   To retrieve all files under the folder Utils:
%
%       >>  dir('C:\matlabR12\work\Interface\Utils')
%
%           .                       Contents.m              
%           ..                      my_interface_evalmcw.m  
%
%   To retrieve recursively the full path of all .m files starting with Cont under the folder work:
%
%       >> fuf('C:\matlabR12\work\Con*.m','detail')
%
%           'C:\matlabR12\work\Laboratorio_modellistica\DTT\Contents.m'
%           'C:\matlabR12\work\Laboratorio_modellistica\SDF\Contents.m'
%           'C:\matlabR12\work\PREPOSTGUIS\Contents.m'
%           'C:\matlabR12\work\PREPOSTGUIS\Utils\Contents.m'
%           'C:\matlabR12\work\Pavia\Contents.m'
%           'C:\matlabR12\work\TwoLe_front_end\Contents.m'
%           [1x74 char]
%           'C:\matlabR12\work\TwoLe_front_end\My_Classes\@Sector\Contents.m'
%           'C:\matlabR12\work\TwoLe_front_end\Utils\Contents.m'
%           'C:\matlabR12\work\Utils\Contents.m'
%
%   To retrieve recursively under the folder PREPOSTGUIS all .fig files and non recursively, under
%   the folder MatlabR12 all .txt files:
%
%       >> fuf({'C:\matlabR12\work\PREPOSTGUIS\*.fig','C:\matlabR12\*.txt'},[1,0],'detail')
%
%               'C:\matlabR12\license.txt'
%               'C:\matlabR12\work\PREPOSTGUIS\Private\MV_Manager.fig'
%               'C:\matlabR12\work\PREPOSTGUIS\R_h.fig'
%               'C:\matlabR12\work\PREPOSTGUIS\TSA.fig'
%               'C:\matlabR12\work\PREPOSTGUIS\Visual_3D.fig'       

%                                         -$-$-$-
%
%        Author:    Francesco di Pierro                  Reasearch Assistant
%                                                    Center for Water Systems (CWS)
%                                                Dep. of Engineering and Computer Science
%                                                        University of Exeter
%                                                    fileExt-mail: <F.Di-Pierro@ex.ac.uk>
%
%                                         -$-$-$-




%--------------------CHECK INPUT ARGUMENT TYPE AND SET DEFAULT VALUES------------------------%
startFolder = pwd;

%argument cheking and parsing default parameters
if nargin < 1
    folders={};
end

if nargin < 2
    isRecurcive=[];
end

if nargin < 3
    opt=[];
end

if nargin < 4
    isIgnoreIllegalFolders=[];
end


%% Default parameter values
if isempty(folders)
    explStartDir=pwd; % start Explorer in current directory
    anotherDir='More';
    while ~strcmpi(anotherDir, 'Finish')
        
        chosenFolder = uigetdir(explStartDir, 'Choose input directory');
        % store last opened directory, to start with it on next Explorer use.
        if isequal(chosenFolder, 0) % If cancel was pressed
            explStartDir=pwd;
            continue;
        end
        
        explStartDir=chosenFolder;
        folders = cat(2, folders, { chosenFolder } );
        
        anotherDir = questdlg({'Need additional folders?',...
            'Press ''More'', to choose additional folder.',...
            'Press ''Finish'' to finish choosing folders.'},...
            'Folders selection',...
            'More', 'Finish','Finish');
    end % while ~strcmpi(anotherDir,'Finish')
end
if ~iscell(folders)
    folders={folders};
end

if isempty(opt)
    opt='normal';
end

if isempty(isRecurcive)
    isRecurcive=false( size(folders) );
end

if isempty(isIgnoreIllegalFolders)
    isIgnoreIllegalFolders=true;
end


%scan folders searching for incorrect folder names and partialpaths!!!

isLegalFolder=true( numel(folders) );
for iFolder=1:length(folders)                                 
    [filePath, fileName, fileExt] = fileparts(folders{iFolder});                    
    if ( ~isdir(filePath) && ~isempty(fileExt) )
        warning(['"', filePath, '" is not a valid folder name!'])
        isLegalFolder(iFolder)=false;
    elseif ( ~isdir(fullfile(filePath, fileName, '') ) && isempty(fileExt))
        warning( strcat('"',fullfile(filePath,fileName,''),...
            '" is not a valid folder name!') );
        isLegalFolder(iFolder)=false;
    elseif ( isempty(dir(filePath)) && ~isempty(fileExt) ) ||...
            ( isempty(dir(fullfile(filePath,fileName,''))) && isempty(fileExt) )
        warning('Matlab PARTIALPATHS not allowed!') 
        isLegalFolder(iFolder)=false;
    else                                                %this cheks the very unlike event arising
        sub = dir(pwd);                                 %when the partial path provided is a directory
        [subel{1:length(sub)}] = deal(sub.name);        %under the current one: this, in fact, is the
        [subtype{1:length(sub)}] = deal(sub.isdir);     %only situation where the dir command handles 
        testel1 = strcmp(subel,fullfile(filePath,fileName,''));       %partialpaths!!!
        testel2 = strcmp(subel,filePath);
        if ~isempty(subtype(testel1)) || ~isempty(subtype(testel2))
            warning('Matlab PARTIALPATHS not allowed!')
            isLegalFolder(iFolder)=false;
        end
    end     % if ( ~isdir(filePath) && ~isempty(fileExt) )
end     % for iFolder=1:length(folders)

if isIgnoreIllegalFolders
    folders=folders(isLegalFolder);
else
    assert( any(~isLegalFolder),...
        sprintf('Illegal folder inputs: %s', folders{~isLegalFolder}) );
end


%--------------------------------CORE FUNCTION--------------------------------%

sortedList = [];                                           %initialize the output list

sortedList = rec(sortedList,folders,isRecurcive,opt);          %call the function

sortedList = sortrows(sortedList);                        %sort the list

if nargout, srtlist = sortedList; else disp (' '),disp(sortedList), end

cd(startFolder);                                            %and set current directory back to the initial one

%--------------------------------RECURSIVE FUNCTION--------------------------------%

function sortedList = rec(sortedList,folders,isRecurcive,opt)

for iFolder=1:length(folders)
    %first build the new search condition made of all files satistying the
    %search condition and all directories under the current one
    val = []; val1 = []; val2 = []; cnval1 = {}; cnval2 = {}; cdval = {}; cdva2 = {};   %initialize helper variables
    [pth,fname,ext] = fileparts(folders{iFolder});            
    cd(pth);                                            %move to the directory: isdir only recognizes directories on the Matlab search path or the current one!
    if isdir(fname)   
        wild = '';
        cd(fname)
        pth = fullfile(pth,fname);
        val = dir;
    else
        %get the filenames satisfying the search condition
        val1 = dir(folders{iFolder});                         
        wild = [fname,ext];
        if isempty(val1)
            cnval1 = [];    cdval1 = [];
        else
            [cnval1{1:length(val1)}] = deal(val1.name); [cdval1{1:length(val1)}] = deal(val1.isdir);
        end
        %and the directory/ies under the current one
        val2 = dir;                                     
        wo = false(size(val2));
        for k=1:length(val2)
            if val2(k).isdir
                wo(k) = 1;
            end
        end
        val2(~wo) = [];
        %and build the new search structure
        [cnval2{1:length(val2)}] = deal(val2.name); [cdval2{1:length(val2)}] = deal(val2.isdir);
        cnval = [cnval1,cnval2];    cdval = [cdval1,cdval2];    
        [val(1:length(cnval)).name] = deal(cnval{:}); [val(1:length(cdval)).isdir] = deal(cdval{:}); 
    end
    for j=1:length(val)
        if (val(j).isdir) && ~any( strcmp(val(j).name, {'.', '..'}) ) 
            %if the jth object under the ith folder is a valid folder name(directory)...
            % not(strcmp(val(j).name,'.')) && not(strcmp(val(j).name,'..'))    
            new_path = fullfile(pth,val(j).name); 
            if ~isdir(new_path)
                continue;
            end
            %set the current directory to that one:
            cd(new_path);                           
            if isRecurcive(iFolder)                            %recursively call the function keeping in mind the search condition
                sortedList = rec(sortedList,{fullfile(new_path,wild)}, isRecurcive(iFolder), opt);
            end
            cd ..                                   %get back to the previous directory                            
        elseif ~any( strcmp(val(j).name, {'.', '..'}) ) 
            %if the jth object under the ith folder is a valid file name
            % not(strcmp(val(j).name,'.')) && not(strcmp(val(j).name,'..')) 
            if strcmp(opt,'detail')
                sortedList =cat( 1, sortedList, {fullfile(pwd, val(j).name)} );
%                 sortedList =cat( 1, sortedList, cellstr(fullfile(pwd,val(j).name)) );
            else
                sortedList =cat( 1, sortedList, {val(j).name} );
%                 sortedList =cat( 1, sortedList, cellstr(val(j).name) );
            end
        end
    end
end

Contact us