How to plot from a line array?

55 visualizzazioni (ultimi 30 giorni)
Andrew Boggiano
Andrew Boggiano il 25 Feb 2020
Modificato: Guillaume il 25 Feb 2020
I'm indexing several lines in a line array as such:
h(i) = plot(B, sim/max(sim));% i = integer
Is there a function that is sort of the reverse of delete(h(i))?
I'm trying to add certain lines to figures together pulling from an array of several lines.
EDIT:
Basically I am looking for a way to easily replot lines that I have already plotted without having to use the plot() function and set the linewidths and colors, etc. all over again. I have found that indexing my lines in a line array has been helpful for removing certain plots by using delete(h), but I haven't found a way to re-add them onto figures after deleting.
  2 Commenti
darova
darova il 25 Feb 2020
The question is unclear. Can you explain more?
Andrew Boggiano
Andrew Boggiano il 25 Feb 2020
Yeah, sorry, updated maintext.

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 25 Feb 2020
Modificato: Guillaume il 25 Feb 2020
Note that what you get out of plot are handles to the lines, not the line objects themselves. In normal circumstances the difference doesn't matter, but if you call delete(h) you destroy the underlying line object and you're left with a handle to an object that is no more. At this point, there's no way to restore it.
If the underlying object still exist (i.e. you haven't deleted it explictly with delete or implicitly by destroying its parent axes (e.g. with a new plot or by closing the figure), you can copy it in a new parent with copyobj.
  3 Commenti
Andrew Boggiano
Andrew Boggiano il 25 Feb 2020
I see, thank you. For some reason I had thought the data was preserved but I must have assigned that handle to another line after deleting it on accident.
Follow up, is there an easy way to remove a line from a spectrum while preserving its data?
Guillaume
Guillaume il 25 Feb 2020
Modificato: Guillaume il 25 Feb 2020
It's not something I'd ever tried, but it turns out that you can reparent a graphics object to an empty graphics placeholder and later reparent it to a new axes. To be honest, I was surprised it works so I'd be wary of how reliable this is:
%hlines: an array of Line objects
%eg
figure; hlines = plot(magic(3));
[hlines(1:2).Parent] = deal([]); %effectively removes the first two lines from the plot.
%can then later on be reparented
figure; hax = gca;
[hlines(1:2).Parent] = deal(hax);
Note that if you're dealing with scalar line objects, you don't need to bother with [] and deal:
%hline: a scalar object
hline.Parent = [];
hline.Parent = ax;

Accedi per commentare.

Più risposte (1)

darova
darova il 25 Feb 2020
Here is an example
clc,clear
% create some data with handles
h(1) = plot([0 1],[0 1],'r');
hold on
h(2) = plot([1 3],[1 2],'b');
h(3) = plot([3 4],[2 3],'g');
hold off
% save data fro h(2)
x = get(h(2),'xdata');
y = get(h(2),'ydata');
axis tight
hold on
pause(1)
delete(h(2:3))
pause(1)
plot(x,y)
hold off
Why do you need such function? Can you show your code?

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by