ShowStructTree.m

by

 

Display the fields of a structure as a tree.

ShowStructTree(run,toprint)
function ShowStructTree(run,toprint)

%Richard Moore
%December 4, 2012

%example usage: ShowStructureTree(run,'run');

%This will be a simple recursive function to display the fields in a
%struct as a tree.  For some reason there doesn't appear to be any easy way
%to do this already.  It explores the tree with a simple depth-first
%method.  

a = whos;

dots = find(toprint=='.');
if numel(dots)>0
    toshow = toprint((dots(end)+1):end);
    disp([char(repmat([124 9],1,numel(dots)-1)) char([124 45 45 45]) toshow]);
else
    disp(toprint);
end  

if isstruct(run)
    namesout = fieldnames(run);
    for x = 1:numel(namesout)
        ShowStructTree(eval([a(1).name '.' namesout{x}]),[toprint '.' namesout{x}]);
    end
end

Contact us