Nesting tiledlayouts within another tiledlayout
263 次查看(过去 30 天)
显示 更早的评论
I would like to created a tiledlayout and, within each of the tiles, start another tiledlayout. I assume this kind of nesting is possible since, in the documentation for TiledChartLayout objects, it says that the parent of a TiledChartLayout can be another TiledChartLayout. However, the following example fails to produce the desired nesting. Instead of nesting the second sub-layout in the second tile, it tries to put it in the first tile. Is it possible to do what I am trying to do? If not, under what circumstances does it make sense to parent a TiledChartLayout to another TiledChartLayout.
m1=[5,6];
m2=[1,1,1,2];
T=tiledlayout(3,1); %Outer layout
nexttile(T,[1,1]); axis off %Next outer tile
t=tiledlayout(T,1,2); %first inner layout
for i=1:2
nexttile(t);
plot(rand(5,m1(i)));
legend('Location','southoutside')
end
nexttile(T,[2,1]); axis off %Next outer tile
t=tiledlayout(T,2,2); %second inner layout
for i=1:4
nexttile(t);
plot(rand(5,m2(i)));
legend('Location','southoutside')
end
0 个评论
采纳的回答
Yemi Ajayi
2021-4-1
编辑:Yemi Ajayi
2021-4-1
Adapting your example:
T=tiledlayout(3,1); %Outer layout
for i=1:3
t=tiledlayout(T,1,1); % Inner layout
t.Layout.Tile = i;
t.Layout.TileSpan = [1 1];
nexttile(t);
plot(rand(5,(i)));
end
lgd = legend('Orientation',"horizontal");
lgd.Layout.Tile = 'south';
Hope that helps.
更多回答(1 个)
James Sweetman
2024-7-24
Here's a crudely implemented class that achieves the same result.
classdef tiledlayoutnested<handle
%TILEDLAYOUTNESTED
properties
root (1,1) {isa(root,'matlab.graphics.layout.TiledChartLayout')}
nested (1,:) {isa(nested,'matlab.graphics.layout.TiledChartLayout'),...
isempty(nested)}
end
methods
% Constructor
function tl = tiledlayoutnested(m,n,varargin)
%%TILEDLAYOUTNESTED
% Constructs a TILEDLAYOUTNESTED object from a top-level 'root'
% TILEDLAYOUT object.
tl.root = tiledlayout(m,n,varargin{:});
end
function nestedtl = nextnest(tl,m,n,varargin)
%%NEXTNEST
% Behaves similar to NEXTTILE in that calling NEXTNEST creates
% a new TILEDLAYOUT object in the next available root tile
% space.
nestedtl = tiledlayout(m,n,'Parent',tl.root,varargin{:});
tl.nested = cat(2,tl.nested,nestedtl);
nestedtl.Layout.Tile = numel(tl.nested);
end
function populate(tl,nest)
%%POPULATE
% Populates all nested tilelayout objects with axes and indexed
% titles for the benefit of the user to visually confirm the
% nested configuration.
if nargin<2
arrayfun(@(i) tl.populate(tl.nested(i)),1:numel(tl.nested))
return
end
ax = arrayfun(@(i) nexttile(nest,i),1:prod(nest.GridSize));
nestidx = find(tl.nested==nest);
arrayfun(@(i) title(ax(i),sprintf('%i.%i',nestidx,i)),1:numel(ax))
end
end
%% Set functions
methods
function set.nested(tl,val)
tl.nested = tl.setnest(val);
end
end
% Set callbacks
methods (Access = private)
function val = setnest(tl,val)
% Clears nested objects in properties
if isempty(val)
delete(tl.nested)
end
end
end
end
Example:
tln = tiledlayoutnested(2,2,'padding','compact');
tl1 = nextnest(tln,1,3,'TileSpacing','compact');
nexttile(tl1)
x = linspace(0,2*pi);
plot(x,sin(x))
nexttile(tl1)
plot(x,sin(x.^2))
tl2 = nextnest(tln,2,2)
tln.populate(tl2)
tln.populate(nextnest(tln,1,1))
tln.populate(nextnest(tln,5,3,'TileSpacing','compact','padding','compact'))
nexttile(tl1)
plot(sin(x),cos(x))
gives the following output:
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!