function sample

[x,y,z] = peaks;

f = figure( ...

'WindowButtonDownFcn', @onMouseDown, ...

'WindowButtonUpFcn', @onMouseUp, ...

'WindowButtonMotionFcn', @onMouseMove );

a = axes('Parent', f);

pcolor(a,x,y,z);

L = line('XData', [], 'YData', [], 'Parent', a);

setappdata(f, 'MouseIsPressed', false);

setappdata(f, 'LineHandle', L);

setappdata(f, 'AxesHandle', a);

setappdata(f, 'FirstLinePoint', [0 0]);

setappdata(f, 'SurfaceData', {x; y; z});

function onMouseDown(src, evt)

setappdata(src, 'MouseIsPressed', true);

setappdata(src, 'FirstLinePoint', ...

get( getappdata( src, 'AxesHandle' ), 'CurrentPoint' ) );

function onMouseUp(src, evt)

setappdata(src, 'MouseIsPressed', false);

L = getappdata(src, 'LineHandle');

x = get( L, 'XData' );

y = get( L, 'YData' );

set( L, 'XData', [], 'YData', [] );

x_interp_pts = linspace(x(1), x(2), 30);

y_interp_pts = linspace(y(1), y(2), 30);

origdata = getappdata(src, 'SurfaceData');

z_interp = interp2( origdata{1}, origdata{2}, origdata{3}, x_interp_pts, y_interp_pts );

figure;

plot(z_interp, '.-');

function onMouseMove(src, evt)

if getappdata(src, 'MouseIsPressed')

firstPoint = getappdata(src, 'FirstLinePoint');

secondPoint = get( getappdata(src, 'AxesHandle'), 'CurrentPoint' );

set( getappdata(src, 'LineHandle'), ...

'XData', [firstPoint(1) secondPoint(1)], ...

'YData', [firstPoint(3) secondPoint(3)] );

end

## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/37017-how-to-allow-user-to-get-an-arbitrary-line-profile-from-a-contourplot#comment_76771

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/37017-how-to-allow-user-to-get-an-arbitrary-line-profile-from-a-contourplot#comment_76771

Sign in to comment.