📄 gtrack.m
字号:
function varargout = gtrack(newVarName,titleFmt)% GTRACK Track mouse position and show coordinates in figure title.% % GTRACK Activates GTRACK. Once it is active the mouse position is% constantly tracked and printed on the figure title. A left-click will% print the coordinates in the command line and store them. Clicking the% mouse right button deactivates GTRACK.% % USAGE% gtrack() tracks the mouse and prints coordinates in the command line.% % clickData = gtrack() will return the click positions in clickData using% UIWAIT. Matlab will be in wait mode until the user finishes clicking.% % gtrack('newVar') tracks the mouse and creates a new variable in the% base workspace called 'newVar' with the click coordinates. This mode% does not use UIWAIT.%% gtrack([],titleFormat) uses titleFormat as the format string for% printing the mouse coordinates in the title.%%% 2007 Jose F. Pina, Portugal% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=15099% % REVISION % 23-05-2007 - created% 30-05-2007 - fixed case matches, nested functions instead of globals,% improved output modes% 31-05-2007 - added option to choose coordinates format%% CREDITS% initial version - based on GTRACE% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3832&objectType=file% Furi Andi Karnapi and Lee Kong Aik, DSP Lab, School of EEE, Nanyang Technological University% Singapore, March 2002%% improved version - thanks for the hints to John D'Errico% http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectType=author&objectId=801347% default format for printing coordinates in titleif nargin<2, titleFmt = '%3.5f'; end% get current figure event functionscurrFcn = get(gcf, 'windowbuttonmotionfcn');currFcn2 = get(gcf, 'windowbuttondownfcn');currTitle = get(get(gca, 'Title'), 'String');% add data to figure handleshandles = guidata(gca);if (isfield(handles,'ID') & handles.ID==1) disp('gtrack is already active.'); return;else handles.ID = 1;endhandles.currFcn = currFcn;handles.currFcn2 = currFcn2;handles.currTitle = currTitle;handles.theState = uisuspend(gcf);guidata(gca, handles);% set event functions set(gcf,'Pointer','crosshair');set(gcf, 'windowbuttonmotionfcn', @gtrack_OnMouseMove); set(gcf, 'windowbuttondownfcn', @gtrack_OnMouseDown); % declare variablesxInd = 0;yInd = 0;clickData = []; % set output modeif nargout, uiMode = 'uiwait'; % use UIWAIT and return to clickData uiwait;elseif nargin && isvarname(newVarName) % dont'e use UIWAIT and assign in caller uiMode = 'nowait'; % workspace to variable newVarNameelse uiMode = 'noreturn'; % dont't use UIWAIT and don't return results (print only)end%% --- nested functions ---------------------------------------------------%% mouse move callbackfunction gtrack_OnMouseMove(src,evnt)% get mouse positionpt = get(gca, 'CurrentPoint');xInd = pt(1, 1);yInd = pt(1, 2);% check if its within axes limitsxLim = get(gca, 'XLim'); yLim = get(gca, 'YLim');if xInd < xLim(1) | xInd > xLim(2) title('Out of X limit'); return;endif yInd < yLim(1) | yInd > yLim(2) title('Out of Y limit'); return;end% update figure titletry title(['X = ' num2str(xInd,titleFmt) ', Y = ' num2str(yInd,titleFmt)]);% possibility of wrong format strings...catch gtrack_Off() error('GTRACK: Error printing coordinates. Check that you used a valid format string.')endend%% mouse click callbackfunction gtrack_OnMouseDown(src,evnt)% if left button, terminateif strcmp(get(gcf,'SelectionType'),'alt') gtrack_Off returnend% else add click to clickDataclickData(end+1).x = xInd;clickData(end).y = yInd;fprintf('X = %f Y = %f\n',xInd,yInd);end%% terminate callbackfunction gtrack_Off(src,evnt)% restore default figure propertieshandles = guidata(gca);set(gcf, 'windowbuttonmotionfcn', handles.currFcn);set(gcf, 'windowbuttondownfcn', handles.currFcn2);set(gcf,'Pointer','arrow');title(handles.currTitle);uirestore(handles.theState);handles.ID=0;guidata(gca,handles);% if there are outputs to assign do soswitch uiMode case 'uiwait' % data return as output argument (clickData) varargout{1} = clickData; uiresume, case 'nowait' % data assigned in base workspace as new variable assignin('base',newVarName,clickData); fprintf('Variable %s assigned with click data.\n',newVarName); case 'noreturn' % nothing to returnendend%% --- end nested functions -----------------------------------------------end % end everything
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -