Legend error. What did I do wrong here?

I have 9 figures as per attached to make comparisons for the length, width and height using similar codes as below (this one was used for the width comparison).
I don't know why the legend for the plots of length and height comparions are correct but it is wrong for the width comparison. How to fix please?
if true
fig = figure(16);
ax = axes(fig);
xlabel('\xi');
ylabel(' \omega(\xi,\tau)');
title('Varying injection rates vs fracture width');
h1 = openfig('width0.fig','reuse');
h2 = openfig('width1.fig','reuse');
h3 = openfig('width2.fig','reuse');
copyobj(h1.Children.Children,ax);
copyobj(h2.Children.Children,ax);
copyobj(h3.Children.Children,ax);
close(h1);
close(h2);
close(h3);
end
hold on
legend('\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast')

1 Comment

% if true
% code
%end
legend([h1(1),h2(2),h3(3)], '\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast')

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 2 Oct 2020
Edited: Adam Danz on 2 Oct 2020
The "width" figures contain multiple lines on top of each other just like in your last question.
To demonstrate, open the width0.fig and look at the number of objects on the axes.
h = openfig('width0.fig');
h.Children.Children
% 2×1 Line array:
%
% Line
% Line
You'll see two line objects. If you only want to copy one of them,
copyobj(h.Children.Children(1),ax);
% ^^^

9 Comments

Is there any way I can copy the both line objects of each width figure and the legend still shows consistant colors as in the original plots?
Yes, specify which handles should appear in the legend.
h1 = copyobj(___);
h2 = copyobj(___);
h3 = copyobj(___);
legend([h1(1), h2(1), h3(1)], ___)
Thanks for your help.
Sorry, I need to reopen this question.
I tried as instructed and this time no legend appeared at all. How to fix please?
Error using legend (line 261)
Invalid argument. Type 'help legend' for more information.
Error in Width (line 18)
legend([h1(1),h2(1),h3(1)], '\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast')
The legend is a child of the figure. You can see the legend handle in h.Children where h is the handle to the figure.
You can copy the legend along with its associated axes but you cannot copy the legend-only. For example,
fig1 = figure();
ax1 = axes(fig1);
plot(ax1, magic(5));
title(ax1, 'Fig 1');
leg = legend(ax1);
fig2 = figure;
h = copyobj([ax1, leg], fig2)
h =
2×1 graphics array: Axes (Fig 1) Legend (data1, data2, data3, data4, data5)
title(h(1), 'Fig 2');
If you want to get the legend handle after the plot is created,
legendHandle = findobj(fig1,'Type','Legend');
Alternatively, you can create recreate the lenged in fig 2 by using the DisplayName properties of graphics objects.
fig1 = figure();
ax1 = axes(fig1);
hold(ax1, 'on')
h(1) = plot(1:5,1:5, 'DisplayName', 'Line 1');
h(2) = plot(1:5,2:6, 'DisplayName', 'Line 2');
h(3) = plot(1:5,3:7, 'DisplayName', 'Line 3');
title(ax1, 'Fig 1');
leg = legend(ax1);
% Create 2nd figure & axes
fig2 = figure;
ax2 = axes(fig2);
% Copy the first 2 lines only
copyobj(h(1:2), ax2)
% Recreate legend using DisplayName properties
legend(ax2)
I don't really follow your instruction.
By copying that independently, you meant the code:
legendHandle1 = findobj(h1,'Type','Legend');
legendHandle2 = findobj(h2,'Type','Legend');
legendHandle3 = findobj(h3,'Type','Legend');
copyobj(legendHandle1, newFigure);
copyobj(legendHandle2, newFigure);
copyobj(legendHandle2, newFigure);
instead of:
copyobj(h1.Children.Children,ax);
copyobj(h2.Children.Children,ax);
copyobj(h3.Children.Children,ax);
Is newFigure here the same as figure(16)?
I updated my comment above.
Sorry I still can't do as you instructed. It seems that I copied all the lines of each object for the legend again and the explanations in it are still unchanged (data 1, data 2...).
Code is as below:
fig1 = figure();
ax1 = axes(fig1);
xlabel('\xi');
ylabel(' \omega(\xi,\tau)');
title(ax1,'Varying injection rates vs fracture width');
leg=legend(ax1,'\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast');
h1 = openfig('width0.fig','reuse');
h2 = openfig('width1.fig','reuse');
h3 = openfig('width2.fig','reuse');
copyobj(h1.Children.Children,ax1);
copyobj(h2.Children.Children,ax1);
copyobj(h3.Children.Children,ax1);
close(h1);
close(h2);
close(h3);
fig2=figure();
h = copyobj([ax1, leg], fig2);
title(h(1),'Varying injection rates vs fracture width');
Result:
"data 1, data2, data 3,..." appears in the legend when the graphics objects do not contain a value for DisplayName as my comment above indicates. If you didn't assign a DisplayName value when you created those lines, then you'll have to recreate the legend from scratch after copying the lines.
fig1 = figure();
ax1 = axes(fig1);
hold on
h1 = openfig('width0.fig','reuse');
h2 = openfig('width1.fig','reuse');
h3 = openfig('width2.fig','reuse');
copyobj(h1.Children.Children,ax1);
copyobj(h2.Children.Children,ax1);
copyobj(h3.Children.Children,ax1);
close(h1);
close(h2);
close(h3);
xlabel('\xi');
ylabel(' \omega(\xi,\tau)');
title(ax1,'Varying injection rates vs fracture width');
leg=legend(ax1,'\alpha=0','\alpha=1/9','\alpha=1/10','location', 'northeast');
This assumes there 3 objects that are copied, since there are 3 strings defined in legend().

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!