No BSD License  

Highlights from
digInto

image thumbnail
from digInto by Chris Rodgers
Search recursively through the fields in a structure

digInto(target, strSearchRE, level)
% [strOut, bFound] = digInto(target, strSearchRE)
%
% target - struct to search
% strSearchRE - regular expression for field name matching
%
% Search recursively through nested structs or cell arrays
% matching field names against a supplied regular expression.
%
% Display data and parents for any match.
%
% Example:
%
% xx = struct('NameOne',1,'AnotherName',2);
% xx.Three.Four.NameOne = { 7 };
% xx.Three.Four.AnotherName = 1;
% digInto(xx,'[Nn]ame')
% digInto(xx,'[Oo]ne')

% Copyright Chris Rodgers, University of Oxford, 2008
% $Id: digInto.m 1413 2008-10-31 10:27:26Z crodgers $

% Inspired by code from: http://code.activestate.com/recipes/576489/
%                Fri, 5 Sep 2008 by kaushik.ghose

function [strOut, bFound] = digInto(target, strSearchRE, level)

if nargin < 3
    level = 0;
end

bFound = 0;
strOut = '';

tabs = repmat(' ',1,4*level);

if iscell(target)
    fn=cell(numel(target),1);
    for idx=1:numel(target)
        fn{idx} = ['{' num2str(idx) '}'];
    end
elseif isstruct(target)
    fn = fieldnames(target);
else
    error('First input must be a cell array or stuct')
end
    
matches = regexp(fn, strSearchRE); % Which fieldnames match?

for n = 1:length(fn)
    if iscell(target)
        fn2 = target{n};
    else
        fn2 = target.(fn{n});
    end
    
    if iscell(fn2) || isstruct(fn2)
        [strOutInner, bFoundInner] = digInto(fn2, strSearchRE, level+1);
        strFoundData = '';
    else
        strOutInner = '';
        % Check this isn't a massive chunk of data!
        fn2_stats = whos('fn2');
        if fn2_stats.bytes < 1024
            strFoundData = [' = ' evalc('disp(fn2)')];
            strFoundData(end) = [];
        else
            strFoundDataTmp = evalc('disp(fn2(1:20).'')');
            strFoundDataTmp(end) = [];
            
            strFoundData = sprintf(' = %d x %s [%s ...]',prod(fn2_stats.size),fn2_stats.class,strFoundDataTmp);
        end
        bFoundInner = 0;
    end

    if any(matches{n}) || bFoundInner
        bFound = 1;
        
        strOut = sprintf('%s%s%s%s\n%s',strOut,tabs,fn{n},strFoundData,strOutInner);
    end
end
end

Contact us at files@mathworks.com