MATLAB Answers

How to edit colors for the line?

1 view (last 30 days)
The legend line corlor is wrong. How do I change the black to green for the measured one?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Aug 2020
We do not know exactly what PlotCone does, but we can predict from the observed behavior that it is creating more than one line() object when it is invoked.
If it can return the line handles, then use
h1 = PlotCone([2000,100,780,50,'k');
h2 = PlotCone(height, Thickness, InnerTopDia, InnerBotDia, [0 0.6 0]);
legend([h1(1), h2(1)], {'Spec', 'Measure'});

  3 Comments

Yingyi Huang
Yingyi Huang on 5 Aug 2020
Itis giving an error says Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
I also tried this:
h1 = PlotCone(2000,100,780,50,'black');
h2 = PlotCone(1916,90.6,796.7,88.8,[0 0.6 0]);
legend([h1,h2],{'Spec','Measure'});
And there is another error appears says
error using PlotCone
Too many output arguments.
But the number of the arguments of the function is correct.function
This is my function:
PlotCone(height, thickness, top_dia, bot_dia, color)
Walter Roberson
Walter Roberson on 6 Aug 2020
ax = gca;
ch0 = get(ax,'Children');
PlotCone(2000,100,780,50,'black');
ch1 = get(ax,'Children');
hold(ax, 'on');
PlotCone(1916,90.6,796.7,88.8,[0 0.6 0]);
ch2 = get(ax, 'Children')
hold(ax, 'off');
pc1_obj = setdiff(ch1, ch0);
pc2_obj = setdiff(ch2, ch1);
pc1_line = findobj(pc1_obj, 'type', 'line');
if isempty(pc1_line)
pc1_line = pc1_obj(1);
else
pc1_line = pc1_line(1);
end
pc2_line = findobj(pc2_obj, 'type', 'line');
if isempty(pc2_line)
pc2_line = pc2_obj(1);
else
pc2_line = pc2_line(1);
end
legend([pc1_line, pc2_line], {'Spec','Measure'})
Because we as outside observers who do not have access to the source code for PlotCone even though we asked for it, do not know what kind of objects PlotCone generates, we have to take the set difference between the axes objects before and after the PlotCone call, to isolate the objects created by PlotCone. Then we have to try to find a line object among that in order to have something to hang the legend on. If we are unable to find a line object, we return the first of the objects that we did find.
Yingyi Huang
Yingyi Huang on 6 Aug 2020
OMG, Thank you so much! Really appreciate your code! It is working now!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by