Viewing multi level structures in the the Workspace
5 views (last 30 days)
Show older comments
I am working with a very complex structured data set with many levels. When I need to "drill down" and see the values four levels deep in the structure, my desktop is littered with the higher levels I had to open just to get down to the one I'm looking at. Is there a setting in MATLAB that allows the previous window to close when the next level is opened. I know this is not always desirable. But I am spending a lot of time closing the trail of breadcrumbs that I don't need.
2 Comments
Jan
on 5 Jun 2018
What exactly is a "data set with many levels"? A nested struct? Why is your desktop filled when you "see the values"? How do you display them? What is the "previous window"?
Answers (1)
OCDER
on 5 Jun 2018
You could use this recursive function to display the variable stored in the Nth level of a structure. This is a starting template code, so you'll have to modify it to fit your needs.
%showNSD will show the nested structure data at the Nth level
%
% EXAMPLE
% clear S
% S.a.b.c1.d.e = 1;
% S.a.b.c1.d.f = 2;
% S.a.b.c1.d.g = 3;
% S.a.b.c2.d = 4;
% S.a.b.c3.d = 5;
%
% >>showNSD(S, 5)
% 1
% 2
% 3
% >>showNSD(S, 4)
% 4
% 5
function showNSD(S, MaxLevel, varargin)
if ~isempty(varargin)
CurLevel = varargin{1};
else
CurLevel = 0;
end
%fprintf('%sLEVEL_%d\n', repmat('.', 1, CurLevel), CurLevel); %Uncomment if you want to see the level #
if CurLevel == MaxLevel
disp(S)
return
elseif ~isstruct(S)
%disp(S) %Uncomment if you want to also show non-struct values for
%levels < MaxLevel
return
end
FieldName = fieldnames(S);
for j = 1:length(FieldName)
showNSD(S.(FieldName{j}), MaxLevel, CurLevel+1);
end
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!