ActiveState Code

Recipe 576489: Matlab code for displaying 'struct' details


This code, when passed a MATLAB structure, will recursively go into it and print out the form of the struct.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function diginto(thestruct, level)

if nargin < 2
    level = 0;
end

fn = fieldnames(thestruct);
for n = 1:length(fn)
    tabs = '';
    for m = 1:level
        tabs = [tabs '    '];
    end
    disp([tabs fn{n}])
    fn2 = getfield(thestruct,fn{n});
    if isstruct(fn2)
        diginto(fn2, level+1);
    end
end
end

Sign in to comment