📄 grabit.m
字号:
end end case 'Delete' % delete the variable from the base workspace btn = questdlg(sprintf('Delete ''%s'' from the workspace?', varName), ... 'Delete from workspace...', 'Yes', 'No', 'No'); switch btn case 'Yes' handles.savedVars = rmfield(handles.savedVars, varName); showAllVarsFcn(handles); evalin('base', sprintf('clear %s;', varName)); case 'No' end endguidata(obj, handles);%--------------------------------------------------------------------------%--------------------------------------------------------------------------% saveDatFcn%--------------------------------------------------------------------------%--------------------------------------------------------------------------function saveDatFcn(pname, fname, var)% this function saves the variable to file[p, fname, ext] = fileparts(fname);switch lower(ext) case '.mat' eval(sprintf('%s = var;', fname)); save(fullfile(pname, [fname, ext]), fname, '-v6'); case '.txt' eval(sprintf('%s = var;', fname)); save(fullfile(pname, [fname, ext]), fname, '-ascii', '-double', '-tabs'); otherwise errordlg('Unknown extension.');end%--------------------------------------------------------------------------%--------------------------------------------------------------------------% selectVariableFcn%--------------------------------------------------------------------------%--------------------------------------------------------------------------function selectVariableFcn(varargin)% this callback is called when a variable is selected from the variable% listbox. When the variable is clicked, it will plot the data in the% Preview Box above. If the variable is double-clicked, it will be opened% in the Array Editor. It also ensures that the variable in the base% workspace is the same copy as the variable stored in the GRABIT% workspace. This means that if you change the contents of a variable in% the base workspace (via other functions), then it will allow you to% update the variable in the GRABIT workspace.obj = varargin{1};handles = guidata(obj);sType = get(handles.GrabitGUI, 'SelectionType');switch sType case {'normal', 'open'} % single or double click vars = fieldnames(handles.savedVars); if isempty(vars) return; end listVal = get(obj, 'value'); varName = vars{listVal}; % check to see if the stored var is the same as the var in the % base workspace if evalin('base', sprintf('exist(''%s'', ''var'')', varName)) % the copy in the base workspace must be a n-by-2 DOUBLE array if strcmp(class(evalin('base', varName)), 'double') && ... ndims(evalin('base', varName)) == 2 && ... size(evalin('base', varName), 2) == 2 if ~isequal(evalin('base', varName), handles.savedVars.(varName)) % may have been modified in the base workspace btn = questdlg(sprintf('''%s'' may have been modified in the base workspace (e.g. Array Editor).\nUpdate the variable?', varName), ... 'Modified Variable', 'Update Base Workspace', 'Update Grabit', 'Neither', 'Update Grabit'); switch btn case 'Update Base Workspace' assignin('base', varName, handles.savedVars.(varName)); case 'Update Grabit' handles.savedVars.(varName) = evalin('base', varName); showAllVarsFcn(handles); set(obj, 'value', listVal); end end else btn = questdlg(sprintf('''%s'' in the base workspace is different from the one in Grabit.\nUpdate the base workspace variable?', varName), ... 'Wrong Variable', 'Update Base Workspace', 'Leave Untouched', 'Update Base Workspace'); switch btn case 'Update Base Workspace' assignin('base', varName, handles.savedVars.(varName)); end end else % the variable does not exist in base workspace, so make a copy assignin('base', varName, handles.savedVars.(varName)); end switch sType case 'normal' % single click axes(handles.PreviewAxis); set(handles.PreviewLine, ... 'xdata', handles.savedVars.(varName)(:, 1), ... 'ydata', handles.savedVars.(varName)(:, 2)); axis auto; case 'open' % double click try openvar(varName); catch errordlg(lasterr); end endendguidata(obj, handles);%--------------------------------------------------------------------------%--------------------------------------------------------------------------% loadImageFcn%--------------------------------------------------------------------------%--------------------------------------------------------------------------function loadImageFcn(varargin)% this function loads an image file[obj, filename] = splitvar(varargin([1, 3]));handles = guidata(obj);if isempty(filename) [fname, pathname] = uigetfile(... {'*.bmp;*.jpg;*.jpeg;*.tif;*.tiff;*.gif;*.png', ... 'Image Files (*.bmp, *.jpg, *.jpeg, *.tif, *.tiff, *.gif, *.png)'; '*.bpm', 'BPM files (*.bpm)'; '*.jpg;*jpeg', 'JPG files (*.jpg, *.jpeg)'; '*.tif;*tiff', 'TIFF files (*.tif, *.tiff)'; '*.gif', 'GIF files (*.gif)'; '*.png', 'PNG files (*.png)'; '*.*', 'All files (*.*)'}, 'Select an image file'); if ischar(fname) filename = fullfile(pathname, fname); else return; endendset(handles.ImageFileLoc, 'string', filename);try %warning off; [A, map] = imread(filename); %warning on;catch errordlg(lasterr); return;endif ndims(A) == 3 %some TIFF files have wrong size matrices. if size(A, 3)>3 A = A(:, :, 1:3); elseif size(A, 3)<3 errordlg('This is a weird image file...possibly a bad TIFF file...'); return; endend% Need this so that image shows up when not called by a CALLBACKset(0, 'ShowHiddenHandles', 'on');cla(handles.PreviewAxis);handles.PreviewLine = line(NaN, NaN, ... 'color' , 'blue', ... 'linestyle' , '-', ... 'marker' , '.', ... 'tag' , 'PreviewLine', ... 'parent' , handles.PreviewAxis);axes(handles.ImageAxis);cla(handles.ImageAxis);NP = get(handles.GrabitGUI, 'nextplot'); % for compatibility with R14if isempty(map) imageInfo = imfinfo(filename); if strcmpi(imageInfo.ColorType, 'grayscale') colormap(gray(2^imageInfo.BitDepth)); endelse colormap(map);endiH = image(A);set(iH, 'HitTest', 'off', 'EraseMode', 'normal');set(handles.ImageAxis, 'xtick', [], 'ytick', []);axis equal;set(handles.GrabitGUI, 'nextplot', NP);set(handles.ImageAxis, ... 'drawmode' , 'fast', ... 'tag' , 'ImageAxis', ... 'handlevisibility', 'callback', ... 'buttondownfcn' , @winBtnDownFcn);set(get(handles.ImageAxis, 'title'), ... 'string' , '', ... 'fontunits' , 'pixels', ... 'fontsize' , 24, ... 'color' , 'red');handles.ImageLine = line(NaN, NaN, ... 'color' , 'red', ... 'linestyle' , 'none', ... 'marker' , '.', ... 'tag' , 'ImageLine', ... 'hittest' , 'off');handles.CalibPtsH(:,1) = line(repmat(NaN, 2, 4), repmat(NaN, 2, 4));handles.CalibPtsH(:,2) = line(repmat(NaN, 2, 4), repmat(NaN, 2, 4));set(handles.CalibPtsH(:,1), ... {'marker','color'}, {'o','r';'o','r';'o','b';'o','b'}, ... 'markersize' , 10, ... 'hittest' , 'off');set(handles.CalibPtsH(:,2), ... {'marker','color'}, {'+','r';'x','r';'+','b';'x','b'}, ... 'markersize' , 20, ... 'hittest' , 'off');% initialize image datahandles.I = A;handles.map = map;handles.CalibVals = [];handles.CalibPts = [NaN, NaN, NaN, NaN];handles.CalibPtsIm = repmat(NaN, 4, 2);handles.ImLimits = [get(handles.ImageAxis, 'xlim'); ... get(handles.ImageAxis, 'ylim')];set(handles.CalibrateImageBtn, ... 'string', 'Calibrate', ... 'enable', 'inactive', ... 'value' , 0);set(handles.GrabPointsBtn, ... 'enable', 'off', ... 'value' , 0);set(handles.ZoomStateBtn, ... 'enable', 'inactive', ... 'value' , 0);zoom off;set(0, 'ShowHiddenHandles', 'off');guidata(obj, handles);%--------------------------------------------------------------------------%--------------------------------------------------------------------------% calibrateImageFcn%--------------------------------------------------------------------------%--------------------------------------------------------------------------function calibrateImageFcn(varargin)% this function performs calibration of the image by prompting the user to% select 4 points on the image as reference points.obj = varargin{1};handles = guidata(obj);if isempty(handles.I) set(obj, 'enable', 'off');else switch get(obj, 'value') case 0 % start calibration set(obj, 'value', 1, 'backgroundcolor', handles.bgcolor4); handles.curPointer = 'custom'; handles.curPointerData.CData = handles.xoPointer; handles.curPointerData.HotSpot = handles.xoPointerHotSpot; set(handles.GrabitGUI, ... 'PointerShapeCData' , handles.curPointerData.CData, ... 'PointerShapeHotSpot' , handles.curPointerData.HotSpot, ... 'WindowButtonMotionFcn' , {@pointerFcn, handles, handles.curPointer}); set([handles.LoadImageBtn, ... handles.SaveAs, ... handles.Rename, ... handles.Delete], ... 'enable', 'off'); set(handles.CalibPtsH, 'xdata', NaN, 'ydata', NaN); handles.CalibVals = []; handles.CalibPts = [NaN, NaN, NaN, NaN]; handles.CalibPtsIm = repmat(NaN, 4, 2); set([handles.hXoValue, handles.hXmValue, ... handles.hYoValue, handles.hYmValue], ... 'string', ' NaN'); % this is the first calibration point: X-Axis Origin handles.curTitle = 'Click on the ORIGIN of x-axis'; set(get(handles.ImageAxis, 'title'), ... 'string', handles.curTitle); % change state to CALIBRATION handles.state = 'calibration'; case 1 % stop (prematurely) calibration set(obj, ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -