Hello! How can i delete a line from my figure?

Hello! I have an image from which i would like to delete a line. How can i do that? When i use the delete() function it does not work. There is no error mentioned nor is the image changing.
Please help!

 Réponse acceptée

Image Analyst
Image Analyst le 28 Déc 2019
This should work
hLine = line(x, y, 'Color', 'r', 'LineWidth', 2);
% Now delete it.
delete(hLine);

9 commentaires

Stany Stone
Stany Stone le 28 Déc 2019
But if my line results from a plot? Will it change anything?
Walter Roberson
Walter Roberson le 28 Déc 2019
You need to find the handle of the object you wish to delete (or set its visibility to off) . findobj() can help for that.
Stany Stone
Stany Stone le 28 Déc 2019
Modifié(e) : Stany Stone le 28 Déc 2019
Ook! Thank you for your answears! So, say i have the plot and then i add findobj(). What do i put inside those parantheses? The structure that i actually have în the plot phase? P
If there is only one line in the plot
hLine = findobj(gca, 'type', 'line');
After which you can delete(hLine) or set(hLine, 'visible', 'off')
If there are multiple lines in the plot, then what (if anything) do you know that is different about the line? For example is it the only line that uses dashes to draw? Is it the only cyan line?
Image Analyst
Image Analyst le 29 Déc 2019
If there are multiple lines, Walter's code will delete all of them. If you don't want to delete all of them, but only want one particular line from a set of multiple lines that are on the graph, then it's best if you just store the handle of that particular one right when it's created, and then pass that one particular handle into delete().
Thank you! But if my code is like this:
xKnown - halfLineLength*cos(perTheta)];
yPerKnown = [yKnown + halfLineLength*
sin(perTheta), ...
yKnown - halfLineLength*sin(perTheta)];
% calculate the ends points of another newL based on the Unknown points of oldL
xPerUnknown = [xUnknown + halfLineLength*cos(perTheta), ...
xUnknown - halfLineLength*cos(perTheta)];
yPerUnknown = [yUnknown + halfLineLength*sin(perTheta), ...
yUnknown - halfLineLength*sin(perTheta)];
% draw these two newL
plot([xPerKnown(1), xPerKnown(2)], [yPerKnown(1), yPerKnown(2)], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3);
plot([xPerUnknown(1), xPerUnknown(2)], [yPerUnknown(1), yPerUnknown(2)], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3)
Then it means that i have to use the hLine=findobj() immediately after the plot of the line i want to delete? And, in this case, the thing that makes the line unique isn't about the width or the color. What should i put in the parantheses to differentiate the one i want?
You can save the result of plot(), then call delete() on those handles when you need to or want to:
% draw these two newL
hPlot1 = plot([xPerKnown(1), xPerKnown(2)], [yPerKnown(1), yPerKnown(2)], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3);
hPlot2 = plot([xPerUnknown(1), xPerUnknown(2)], [yPerUnknown(1), yPerUnknown(2)], 'o-', ...
'Color', darkGreen, 'MarkerSize', 24, 'LineWidth', 3)
% More code.... then later, when you want to delete them....
delete(hPlot1); % Delete line created from plot().
delete(hPlot2); % Delete line created from plot().
Walter Roberson
Walter Roberson le 29 Déc 2019
Yes, recording the handle will always be a better solution rather than findobj when it can be done. Sometimes though you are wanting to delete a line from a plot created by code you do not have control of, or from a stored fig that you cannot change the generation code for, and in those cases you would use findobj or findall
Stany Stone
Stany Stone le 30 Déc 2019
Thnak you so much for your answears!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by