How to keep lines on a changing plot ?

6 views (last 30 days)
Adrien
Adrien on 5 Nov 2014
Answered: Mike Garrity on 5 Nov 2014
Hello,
I have an axe :
S.a_axe = axes(...);
On this axe I'm plotting some data, but I'm making it in a loop, so that every couple of seconds another data set is ploted. So the image is changing all the time. I would like to plot on it lines, but the problem is, that this lines must be always on the plot. Is there an easy way to plot lines on a plot. I mean to set them as the first seen object. Or do I have to create them always new after every plot.
Thank you, Adrien

Accepted Answer

Adam
Adam on 5 Nov 2014
Do you always have an image on the axes?
If so then you don't need to keep replacing the image (and by the way, be careful using hold on with images...I have done that before without thinking and you can end up with 10 images all one on top of another taking up memory even though you only see the top one!).
If you keep the image handle as e.g.
hImage = image( S.a_axe, ... );
you can then replace the data in the image with e.g.
hImage.CData = newImageData;
and it will simply replace the data without changing anything else, including anything else also plotted on the axes.
You will still need to do some replotting of your line the first time though if you plot the line before the first image as the first image will sit on top of the line. But you can follow orion's advice for this initial case of reordering children.

More Answers (2)

Orion
Orion on 5 Nov 2014
use the hold function
S.a_axe = axes();
hold on;
for i=1:10
plot(i*sin(0:0.01:10));
end
by default matlab replace the plot at each call of plot, unless you hold it.
  2 Comments
Adrien
Adrien on 5 Nov 2014
But my Problem is, the lines are created and ploted on the axe as first. And than i want in a loop, plot something into the axes. When I ploting my image and than creating the line but holding the image, yes it works, but my problem is that the image is the one which is always removed and reploted. And the lines got to be on the image ... I thought maybe I can make an other axes at the same place with the lines on it but a transparent background.
Orion
Orion on 5 Nov 2014
You could play with the order of the children in the axe.
% create an axe and some plots.
axes();
hold on;
for i=1:10
plot(i*100*sin(0:0.01:10));
end
pause(1)
% then add an image : will be over the lines
image(imread(which('street1.jpg')));
pause(1)
% inverse the children of the current axe
set(gca,'children',flipud(get(gca,'children')))

Sign in to comment.


Mike Garrity
Mike Garrity on 5 Nov 2014
Probably the simplest approach would be to save a handle to the image object and then just set its CData when you want to change the image.
h = image(first_img)
hold on
for i=1:10
plot(...)
end
pause(1)
set(h,'CData',next_img)
In this way, the order of the image won't change relative to the other things you plotted. All that's changing is the contents of the image.

Community Treasure Hunt

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

Start Hunting!