Plotting graph over image

Hello all I have plotted an image on matlab. I have another plot of a single cross that I want to appear over the image at the height inputted by the user. The code can be seen below. Currently the image appears then disappears for the cross to be plotted in the same figure window however I want the image to remain and the cross to appear on the image. I think it could be an issue with 'hold off' but I have tried this and can't crack the problem, any help would be greatly appreciated.
if true
altitude = 'Please enter the altitude in metres you wish to detonate: ';
altitude = input(altitude);
if (altitude<=100);
disp('This is an aerial burst estimator, please enter a number greater than 100 metres.')
elseif (altitude>100000);
disp('The Test Ban Treaty of 1963 prohibits nuclear weapons tests "or any other nuclear explosion"')
disp('in the atmosphere, in outer space, and under water.')
return
end
clf
rgb = imread('altitude4.png');
imshow(rgb)
hold on
M = size(rgb,1);
N = size(rgb,2);
for k = 1:25:M
x = [1 N];
y = [k k];
plot(x,y,'Color','w','LineStyle','-');
plot(x,y,'Color','k','LineStyle',':');
end
for k = 1:25:N
x = [k k];
y = [1 M];
plot(x,y,'Color','w','LineStyle','-');
plot(x,y,'Color','k','LineStyle',':');
end
hold
x = 0.5;
y = altitude;
plot(x,y,'*')
ylim([0 750])
xlim([0 1])
ylabel('Altitude (meters)');
help hold
end

 Respuesta aceptada

Guillaume
Guillaume el 22 de Mzo. de 2016
Editada: Guillaume el 22 de Mzo. de 2016

2 votos

By default, plot delete existing objects (image, other plots, etc.) from the axes. To turn that behaviour off you must have hold set to 'on' every time you plot.
Note that issuing just
hold
toggles the state of hold. As posted your code is:
%...
imshow(rgb)
hold on
%... several plots in a loop that are going to be plotted on top of the image
% since hold is on
hold %TOGGLE THE STATE OF HOLD! so new plot erase content
%...
plot(x, y, '*') %erase content of axes since hold is off
Solution: get rid of the single hold before you plot your altitude.
There's also the problem that you change the xlim to [0 1] which will result in the axes only showing one column of your image. There is no need for you to change xlim or ylim.

9 comentarios

Ogen
Ogen el 22 de Mzo. de 2016
thanks for the reply. Are you saying remove the 'hold' because when I do this it still removes the image and the plot no longer looks the same
Ogen
Ogen el 22 de Mzo. de 2016
Removing the x and y lim plots the cross over the image, which is what i want. However I want it to be plotted in the middle of the image in x axis direction (middle of the x axis) but it plots it on the y axis as if x is 0
Ogen
Ogen el 22 de Mzo. de 2016
Setting x to 600 plots it in the middle of the x axis , which is what I want. However its a picture of space, sky and ground to display altitude. If the user inputs 250 metres it plots an X in the sky region (realistic) but if the user inputs 50 metres it plots it in the space region not the ground region (opposite way round). I think it is to do with the grid system do you know how to change this?
Guillaume
Guillaume el 22 de Mzo. de 2016
With images, the coordinate system is y pointing down. Simply subtract the image height from the altitude to plot it where you want:
plot(x, size(rgb, 1) - altitude, '*');
Ogen
Ogen el 22 de Mzo. de 2016
Nice! Thank you very much it's now working
Ogen
Ogen el 22 de Mzo. de 2016
Do you know how to increase the image as the highest value that can be inputted and still appear is around 475m. Would it be possible to increase it so the user could input up to 1000m?
Guillaume
Guillaume el 22 de Mzo. de 2016
Just scale your altitude so that it is equal to the image height at maximum altitude:
plot(x, size(rgb, 1)*(1-altitude/maxaltitude), '*');
Image Analyst
Image Analyst el 22 de Mzo. de 2016
Tom: If it's working you can "Accept" and vote for Guillaume's answer to "thank" him by giving him reputation points.
If you still need help, post a screenshot so we can see what you're talking about better.
Ogen
Ogen el 23 de Mzo. de 2016
Thanks for your help. Would it be possible to make the star animated to produce a flashing effect?

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 22 de Mzo. de 2016

Comentada:

el 23 de Mzo. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by