% This functions craetes a figure window and allows user to % pick an arbitrary interface. % Pressing the spacebar terminates the sequence of points. % Parameters: % width and hight are the dimensions of the area, % Returns: % layer is array of picked points (each row cotains x and y of picked point) function [layer] = picklayer(width,height) layer = []; % initial empty array of points fig = figure(1); while true % infinite loop waiting for user inputs clf hold on; axis([0,width,0,height]); % plot range set(gca,'YDir','reverse'); % reverse the vertical axis xlabel("Distance (m)"); ylabel("Depth (m)"); % plot line and nodes if size(layer,1) > 1 plot(layer(:,1),layer(:,2),'b-'); end if size(layer,1) > 0 plot(layer(:,1),layer(:,2),'bo'); end % read mouse position (x,y) and key ptessed by the user [ x, y, key ] = ginput(1); % interpret user's action if x < 0 || x > width || y < 0 || y > height % outside of plot range break end switch key case 32 % spacebar pressed - drop out of the loop break case 1 % left mouse button - pick a new point if isempty(layer) % doing first point layer = [x,y]; else % need to insert a point % add a point at the end and then sort by X layer = [ layer; x, y ]; [xsort, ind] = sort(layer(:,1)); layer = layer(ind,:); end otherwise continue end % switch end % big loop close(fig); end % function