Error: Children may only be set to a permutation of itself
Show older comments
When I use the following commands in a subplot environment
getChildren = get(gca,'Children');
set(gca,'Children',[getChildren(4:5); getChildren(1:3)]) % I use this command to reshuffle the several objects in my plot (similarly to uistak)
Things work in the first subplot, i.e. suplot(1,2,1). But, when Matlab goes to the second subplot, i.e. subplot(1,2,2), it is not able to produce my graphics and gives me the following error:
Error using matlab.graphics.axis.Axes/set
Children may only be set to a permutation of itself.
Error in MyFile (line 215)
set(gca,'Children',[getChildren(4:5); getChildren(1:3)])
Do you have any suggestion to avoid/solve this error?
Here following a simplified code:
% Graph
s = [1 1 1 3 3 6 7 8 9 10 4 12 13 5 15 16 17 18 19 19 20 20 17 24 25 4 27 28 29];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30];
G = graph(s,t);
% Node ID: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
G.Nodes.X = [2 1 3 2 4 4 5 4 5 4 5 1 3 3 5 6 7 6 8 7 9 8 9 8 9 10 1 1 0 1]';
G.Nodes.Y = [2 1 3 9 3 5 8 12 13 18 21 15 18 21 0 2 8 12 15 20 10 22 18 5 4 4 5 8 12 23]';
% Subgraphs
Gpath{1} = subgraph(G,shortestpath(G,4,14));
Gpath{2} = subgraph(G,shortestpath(G,4,26));
Gpath{3} = subgraph(G,shortestpath(G,4,30));
Gpath{4} = subgraph(G,shortestpath(G,3,10));
Gpath{5} = subgraph(G,shortestpath(G,3,28));
Gpath{6} = subgraph(G,shortestpath(G,3,21));
Gpath{7} = subgraph(G,shortestpath(G,17,12));
Gpath{8} = subgraph(G,shortestpath(G,17,23));
Gpath{9} = subgraph(G,shortestpath(G,17,26));
% Figure
for k = 1 : 2
subplot(3,2,k);
hold on
p(1) = plot(G,'XData',G.Nodes.X,'YData',G.Nodes.Y,'LineWidth',1,'EdgeColor','k','NodeColor','k');
for i = [1 2 3]
p(2) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','y','NodeColor','y');
p(2).NodeLabel = {};
p(2).EdgeAlpha = 1;
p(2).LineWidth = 5;
% p(2).DisplayName = 'Banana';
end
for i = [7 8 9]
p(3) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','g','NodeColor','g');
p(3).NodeLabel = {};
p(3).EdgeAlpha = 1;
p(3).LineWidth = 9;
p(3).DisplayName = 'Apple';
end
for i = [4 5 6]
p(4) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','r','NodeColor','r');
p(4).NodeLabel = {};
p(4).EdgeAlpha = 1;
p(4).LineWidth = 15;
p(4).DisplayName = 'Strawberry';
end
text(8,.2,'hello')
plot([0.5 0.6],[0 6],'color',[0.6 0.6 0.6],'LineWidth',5);
rectangle('Position',[7,1,2,0.5],'FaceColor',[0.4 0.4 0.4]);
a = get(gca,'Children');
b = findobj('Type','GraphPlot');
idx1 = find(~cellfun(@isempty,{b.DisplayName})); % GraphPlots with name
idx2 = find(cellfun(@isempty,{b.DisplayName})); % GraphPlots without name
c = vertcat(flipud(b(idx2)),flipud(b(idx1)));
set(gca,'Children',[c; setdiff(a,b)])
legend(c)
end
% The error that I get in my machine with the above mentioned code:
Error using matlab.graphics.axis.Axes/set
Children may only be set to a permutation of itself.
Error in uistack_graph_subgraph_2 (line 61)
set(gca,'Children',[c; setdiff(a,b)])
2 Comments
Ganesh
on 17 Jun 2024
Could you please share the entire code you are using?
Accepted Answer
More Answers (1)
Sim
on 17 Jun 2024
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!

