Clear Filters
Clear Filters

Show line above surf plot in 2D view

8 views (last 30 days)
Jacob
Jacob on 6 Aug 2014
Commented: Ben11 on 11 Aug 2014
Hello,
I'm trying to modify ginput such that it will allow the following:
I have a 2D matrix. I plot the values in an axes using "surf" with a custom colormap. This part works.
Click a point on the graph. Move the mouse. Draw a line, connecting the first point click to the current position of the mouse.
The problem is, even with my efforts to draw the line in front, it shows up hidden behind my plot.
This is the code that executes in ginputmodded after the first click:
if isempty(out1)
lineconnect = line('LineWidth',5, ...
'XData',[pt(1,1),pt(1,1)], 'YData',[pt(1,2),pt(1,2)])
v = allchild(gca)
vsize = length(v);
lineposinv = find(v == lineconnect)%index of lineconnect in v
uistack(lineconnect,'up', lineposinv-1)%move lineconnect to front
v = allchild(gca)
linestore(lineconnect);
end
The following executes when the mouse is moved (WindowButtonMotionFcn):
function dummy()
pts = pointstore;
if ~isempty(pts) %block only executes after a point is clicked
lineconnect = linestore();
cp = get(gca,'CurrentPoint');
set(lineconnect,'XData',[pts(1,1),cp(1,1)],...
'YData',[pts(1,2),cp(1,2)]);
v = allchild(gca)
vsize = length(v);
lineposinv = find(v == lineconnect)%index of lineconnect in v
uistack(lineconnect,'up', lineposinv-1)%move to front
v = allchild(gca)
linestore(lineconnect);
end
end
linestore and pointstore are helper functions that store a persistent variable. They work fine.
Any ideas how I could make this work properly? I'm using MATLAB R2014a on Windows 7.

Accepted Answer

Jacob
Jacob on 8 Aug 2014
I crossposted this on StackOverflow and got a solution from user patrik. Thought I'd share it here in case anyone else comes looking. Turns out using surf with view(2) still preserves the z-axis. So I just plotted the connecting line with 'ZData' at 2^16, guaranteed larger than any of my plot values. Works splendidly.
"Ok, I am not sure this solves the problem, but it likely will. The thing with 2D view: This is only another perspective of the plot. The data will still have 3D coordinates. I am not entirely sure, but if I recall correctly, ginput gives the coordinates in xy. This means that if you want to display a line above the surf plot you need to set its z-coordinate to larger than maximum surf value."

More Answers (1)

Ben11
Ben11 on 6 Aug 2014
You could try to inverse the order of your axes' children after drawing the line, so that it comes up at the front and the plot stays at the back:
set(gca,'children',flipud(get(gca,'children')));
I can't test it now though so it might very well not work in your case :)
  1 Comment
Jacob
Jacob on 6 Aug 2014
Nope, didn't work, the line still appears behind the plot.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!