Editor's Note: This file was selected as MATLAB Central Pick of the Week
This function is very simple and recursively displays the fields and sub-fields of a Matlab structure using the built-in 'disp' function.
Thomas Deneux (2021). Structure display (https://www.mathworks.com/matlabcentral/fileexchange/13831-structure-display), MATLAB Central File Exchange. Retrieved .
Inspired: PRINTSTRUCT | Recursively print structures to the command window or a cell char array, export_fig
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Am using this nice function to display all contents of a very complex struc called SPM, produced by the neuroimaging software SPM. At some point in the iteration, the following errors interrupt the function:
Error using sub2ind (line 29)
Size vector must have at least 2 elements.
Error in file_array/subsref>multifile2mat (line 153)
ind = sub2ind(sobj.dim,indx2{:});
Error in file_array/subsref>subfun (line 98)
t = multifile2mat(sobj,varargin{:});
Error in file_array/subsref (line 65)
t = subfun(sobj,args{:});
Error in fn_structdisp>rec_structdisp (line 86)
rec_structdisp([subnames{i} '(' num2str(k) ')'],a(k))
Error in fn_structdisp>rec_structdisp (line 83)
rec_structdisp(subnames{i},a)
Error in fn_structdisp>rec_structdisp (line 86)
rec_structdisp([subnames{i} '(' num2str(k) ')'],a(k))
Error in fn_structdisp>rec_structdisp (line 83)
rec_structdisp(subnames{i},a)
Error in fn_structdisp (line 28)
rec_structdisp(Xname,X)
Any idea how to edit the script so that any type of structure can be fully parsed? Thanks!
Great!! IT also shows the matrices, which was what I was looking for. Thanks!
This is awesome!
How can I submit an improvement? (I mean, I improved your code a little and would like to share)
This is awesome!
How can I submit an improvement?
This is what I want! good job.
a mnemonic after matlab's builtin celldisp:
function varargout = structdisp (varargin)
[varargout{1:nargout}] = fn_structdisp (varargin{:});
end
Does just what I wanted
Comments from B. Roossien and Jeff have been adressed in the June 2012 version.
very nice for me!! thanks!
Useful for me
Love the concept... A couple of notes:
1) I'd recommend creating an else condition to the 'if' that starts on line 49. I'm running into a case where nsub is never initialized if you set CELLRECURSIVE to false and has an error.
2) I'd recommend changing on line 90 size(a,1)>1 to (size(a,1)>1 || size(a,2)>1) so that it can display row vectors as well although they do display a little messy.
simple and neat
Looks good
Display could be a little improved and some diplayed line could be omitted (nested nested nested nested structures....) but all fields are displayed and one can quickly know where is the field
Quick and effective display of structs
No H1-line, no help, no comments, no array support. Too simple