matlablineginput

Show line above surf plot in 2D view


Reposting from Matlab Answers due to lack of interest and a need for an answer.

I have a 2D matrix. I plot the values in an axes using "surf" with a custom colormap and 2D view. This part works. I'm trying to modify ginput such that it will allow the following:

  1. Click a point on the graph.
  2. Move the mouse.
  3. 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.


Solution

  • 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.