📄 ruler.m
字号:
function varargout = ruler(varargin)
%RULER Ruler management function.
% Copyright (c) 1988-98 by The MathWorks, Inc.
% $Revision: 1.33 $
% API - state of ruler is stored in a structure in the userdata
% of the figure in which it is installed - ud.ruler
%
% field description
% ----- -----------
% color string (evals to color spec in base workspace),
% defines color of ruler lines and labels
% marker string ('o','+',etc)
% markersize string ('5','4','m',etc - evaled in base workspace)
% value current value of rulers
% x1, x2, y1, y2, dx, dy, dydx
% type 'vertical' 'horizontal' 'track' or 'slope'
% lines [l1 l2 slopeline] handles of ruler lines
% markers [m1 m2] handles of ruler marker lines
%
% ud.focusline handle of current line being measured (for 'track' and 'slope')
% (empty if no focus)
if nargin < 1
action = 'init';
else
action = varargin{1};
end
switch lower(action)
%-----------------------------------------------------------------
% curs = ruler('motion',fig)
% returns 1 if currentpoint is over ruler 1, 2 if over ruler 2, 0 else
%
case 'motion'
fig = varargin{2};
ud = get(fig,'userdata');
mainaxes = ud.mainaxes;
p = get(mainaxes,'currentpoint');
p = p(1,1:2);
xlim = get(mainaxes,'xlim');
ylim = get(mainaxes,'ylim');
if ~pinrect(p,[xlim ylim]) % outside of main axes
curs = 0;
else
if all(isnan([ud.ruler.value.x1 ud.ruler.value.x2 ...
ud.ruler.value.y1 ud.ruler.value.y2]))
curs = 0;
varargout{1} = curs;
return
end
mpos = get(mainaxes,'position'); % in pixels
% alternate method (doesn't quite work)
%fp = get(fig,'currentpoint');
%fprintf(1,'x=%d, y=%d, ',fp(1),fp(2))
%fprintf(1,' xlim(1)=%d, xlim(2)=%d ',mpos(1),mpos(1)+mpos(3)-1)
%x1 = ((ud.ruler.value.x1-xlim(1))*(mpos(3)-1)/diff(xlim)) ...
% + mpos(1) - 1;
%fprintf(1,' x1=%4.3f',x1)
%dist = abs(x1-fp(1));
%fprintf(1,' dist=%d\n',dist)
if ud.ruler.type(1)=='h' % horizontal
if strcmp(get(mainaxes,'yscale'),'log')
% if (ylim(1) == 0), ylim(1) = eps*ylim(2); end
ppd = mpos(4)/diff(log10(ylim)); % pixels per decade
y1 = ud.ruler.value.y1;
y2 = ud.ruler.value.y2;
c1 = 10^(-3.5/ppd);
c2 = 10^(2.5/ppd);
if (p(2)>=c1*y1) & (p(2)<=c2*y1)
curs = 1;
elseif (p(2)>=c1*y2) & (p(2)<=c2*y2)
curs = 2;
else
curs = 0;
end
else
five_ypixels = 3.5*diff(ylim)/mpos(4);
if abs(p(2)-ud.ruler.value.y1)<=five_ypixels
curs = 1;
elseif abs(p(2)-ud.ruler.value.y2)<=five_ypixels
curs = 2;
else
curs = 0;
end
end
else % vertical
if strcmp(get(mainaxes,'xscale'),'log')
% if (xlim(1) == 0), xlim(1) = eps*xlim(2); end
ppd = mpos(3)/diff(log10(xlim)); % pixels per decade
x1 = ud.ruler.value.x1;
x2 = ud.ruler.value.x2;
c1 = 10^(-2.5/ppd);
c2 = 10^(3.5/ppd);
if (p(1)>=c1*x1) & (p(1)<=c2*x1)
curs = 1;
elseif (p(1)>=c1*x2) & (p(1)<=c2*x2)
curs = 2;
else
curs = 0;
end
else
five_xpixels = 3.5*diff(xlim)/mpos(3);
if abs(p(1)-ud.ruler.value.x1)<=five_xpixels
curs = 1;
elseif abs(p(1)-ud.ruler.value.x2)<=five_xpixels
curs = 2;
else
curs = 0;
end
end
end
end
varargout{1} = curs;
%-----------------------------------------------------------------
% ruler
% ruler('init',fig)
% ruler('init',fig,rulerPopupStr,rulerPopupVal,popupCallback,allAxesList)
% startup code; adds ruler and popup to choose focus line to gcf
% Inputs:
% fig - figure of client (ie. filtview, sigbrowse, sepctview)
% rulerPopupStr - names of all possible subplots
% rulerPopupVal - value of popupmenu reflecting the currently
% selected plot
% popupCallback - callback to ruler popupmenu
% allAxesList - list of possible handle values for ud.mainaxes, default
% is just ud.mainaxes
%
case 'init'
% save_fig = gcf;
fig = gcf;
rulerPopupStr = {''};
rulerPopupVal = 1;
popupCallback = '';
if nargin > 1
fig = varargin{2};
end
if nargin > 2
rulerPopupStr = varargin{3};
end
if nargin > 3
rulerPopupVal = varargin{4};
end
if nargin > 4
popupCallback = varargin{5};
end
% figure(fig)
ud = get(fig,'userdata');
if nargin > 5
rul.allAxesList = varargin{6};
else
rul.allAxesList = ud.mainaxes;
end
rul.type = ud.prefs.ruler.type;
rul.value.x1 = NaN;
rul.value.y1 = NaN;
rul.value.x2 = NaN;
rul.value.y2 = NaN;
rul.value.dx = NaN;
rul.value.dy = NaN;
rul.value.dydx = NaN;
rul.varname = 'r'; % used in 'Save measurements' dialog box
ud.ruler = rul;
ud.prefs.tool.ruler = 1;
uibgcolor = get(0,'defaultuicontrolbackgroundcolor');
uifgcolor = get(0,'defaultuicontrolforegroundcolor');
mainaxes = ud.mainaxes;
% Define axes frame properties: (these axes act as frames)
ax_props = {
'parent',fig,...
'units','pixels',...
'box','on',...
'xcolor','k',...
'ycolor','k',...
'color',uibgcolor,...
'xtick',[],...
'ytick',[],...
'handlevisibility','callback' };
% Define Text Label properties:
label_props = {
'color',uifgcolor,...
'fontname',get(0,'defaultuicontrolfontname'),...
'fontsize',get(0,'defaultuicontrolfontsize'),...
'fontweight',get(0,'defaultuicontrolfontweight'),...
'handlevisibility','callback' };
ui_label_props = {
'units','pixels',...
'parent',fig,...
'style','text',...
'horizontalalignment','right',...
'handlevisibility','callback' };
line_props = {
'parent',mainaxes,...
'visible','off',...
'erasemode','xor',...
'xdata',0,'ydata',0,...
'color',evalin('base',ud.prefs.ruler.color),...
'handlevisibility','callback' };
markersize = evalin('base',ud.prefs.ruler.markersize);
% create line 2 and marker 2 first, so buttondown callbacks will always
% favor line 1.
l2 = line(line_props{:},'tag','ruler2line','linestyle','--',...
'linewidth', 1, ...
'buttondownfcn','sbswitch(''ruldown'',2)');
m2 = line(line_props{:},'tag','ruler2marker',...
'buttondownfcn','sbswitch(''ruldown'',2)',...
'linestyle','none','marker',ud.prefs.ruler.marker,...
'markersize',markersize);
l1 = line(line_props{:},'tag','ruler1line',...
'buttondownfcn','sbswitch(''ruldown'',1)',...
'linewidth', 1);
slopeline = line(line_props{:},'tag','slopeline',...
'linewidth',1,'linestyle','--');
m1 = line(line_props{:},'tag','ruler1marker',...
'buttondownfcn','sbswitch(''ruldown'',1)',...
'linestyle','none','marker',ud.prefs.ruler.marker,...
'markersize',markersize);
peakline = line(line_props{:},'tag','peakline',...
'buttondownfcn','sbswitch(''ruldown'',1)',...
'linestyle','none','marker','^','visible','off');
valleyline = line(line_props{:},'tag','valleyline',...
'buttondownfcn','sbswitch(''ruldown'',1)',...
'linestyle','none','marker','v','visible','off');
ud.ruler.lines = [l1 l2 slopeline peakline valleyline];
ud.ruler.markers = [m1 m2];
% ====================================================================
% Ruler axes - contains rulers
ud.ruler.hand.ruleraxes = axes(ax_props{:}, 'tag','ruleraxes');
% ====================================================================
% Ruler label
ud.ruler.hand.rulerlabel = text(label_props{:},...
'parent',ud.ruler.hand.ruleraxes,...
'horizontalalignment','center',...
'tag','rulerlabel','string','Rulers');
% ====================================================================
% Ruler frame line
line_frame_props = {
'color','k',...
'handlevisibility','callback' };
ud.ruler.hand.rulerframe = ...
line(line_frame_props{:},'parent',ud.ruler.hand.ruleraxes,...
'tag','rulerframe');
% ====================================================================
% Ruler - Labels for ruler values
ud.ruler.hand.x1label = uicontrol(ui_label_props{:},...
'tag','x1label','string','x1');
ud.ruler.hand.y1label = uicontrol(ui_label_props{:},...
'tag','y1label','string','y1');
ud.ruler.hand.x2label = uicontrol(ui_label_props{:},...
'tag','x2label','string','x2');
ud.ruler.hand.y2label = uicontrol(ui_label_props{:},...
'tag','y2label','string','y2');
ud.ruler.hand.dxlabel = uicontrol(ui_label_props{:},...
'tag','dxlabel','string','dx');
ud.ruler.hand.dylabel = uicontrol(ui_label_props{:},...
'tag','dylabel','string','dy');
ud.ruler.hand.dydxlabel = uicontrol(ui_label_props{:},...
'tag','dydxlabel','string','m');
% ====================================================================
% Ruler editboxes 1 and 2
edit_props = {'units','pixels','style','edit','backgroundcolor','w','string','-',...
'horizontalalignment','left','parent',fig};
ud.ruler.hand.boxes(1) = uicontrol(edit_props{:},'tag','rulerbox1',...
'callback','sbswitch(''ruler'',''rulerbox'',1)');
ud.ruler.hand.boxes(2) = uicontrol(edit_props{:},'tag','rulerbox2',...
'callback','sbswitch(''ruler'',''rulerbox'',2)');
% ====================================================================
% Text UIControls to display values of rulers
text_props = {'style','text','backgroundcolor',uibgcolor,'parent',fig,...
'foregroundcolor',uifgcolor,'horizontalalignment','left',...
'string','-','units','pixels'};
ud.ruler.hand.y1text = uicontrol(text_props{:},'tag','y1text');
ud.ruler.hand.y2text = uicontrol(text_props{:},'tag','y2text');
ud.ruler.hand.dxtext = uicontrol(text_props{:},'tag','dxtext');
ud.ruler.hand.dytext = uicontrol(text_props{:},'tag','dytext');
ud.ruler.hand.dydxtext = uicontrol(text_props{:},'tag','dydxtext');
% ====================================================================
% Ruler 1 button
ud.ruler.hand.buttons(1) = uicontrol(...
'units','pixels',...
'parent',fig,...
'style','pushbutton',...
'string','1',...
'callback','sbswitch(''ruler'',''rulerbutton'',1)',...
'tag','ruler1button' );
% ====================================================================
% Ruler 2 button
ud.ruler.hand.buttons(2) = uicontrol(...
'units','pixels',...
'parent',fig,...
'style','pushbutton',...
'string','2',...
'callback','sbswitch(''ruler'',''rulerbutton'',2)',...
'tag','ruler2button' );
% ====================================================================
% Save Ruler button
ud.ruler.hand.saverulerbutton = uicontrol(...
'units','pixels',...
'parent',fig,...
'style','pushbutton',...
'string','Save Rulers...',...
'callback','sbswitch(''ruler'',''save'')',...
'tag','saverulerbutton' );
%====================================================================
% Popup to select the subplot the rulers will focus on
if (length(rulerPopupStr) > 1)
pop_props = {'units','pixels',...
'style','popup','horizontalalignment','left'};
% Position is set in ruler('resize')
ud.ruler.hand.rulerpopup = uicontrol(pop_props{:},...
'units','pixels',...
'parent',fig,...
'string',rulerPopupStr,...
'tag','rulerpopup',...
'callback', popupCallback,...
'value',rulerPopupVal);
end
% ====================================================================
% toolbar for rulers
% 'rulergroup' - includes Vertical, Horizontal, Track, Slope
% common text properties:
tp = [',''color'',''k'',''fontunits'',''pixels'',' ...
'''horizontalalignment'',''center'',''fontsize'',9'];
s1 =['[line([.3 .3],[.4 .95],''color'',''k'') ' ...
' line([.7 .7],[.4 .95],''linestyle'',''--'',''color'',''k'') '...
' text(.5,.15,''Vertical''' tp ')]'];
s2 =['[text(.5,.15,''Horizontal''' tp ')'...
' line([.1 .9],[.8 .8],''color'',''k'') ' ...
' line([.1 .9],[.5 .5],''linestyle'',''--'',''color'',''k'') ]'];
s3 =['[text(.5,.15,''Track''' tp ')'...
' line([.3 .3],[.4 .95],''color'',''k'') ' ...
' line([.7 .7],[.4 .95],''linestyle'',''--'',''color'',''k'')' ...
' line([.3 .7],[.5 .8],''linestyle'',''none'',''marker'',''o'','...
'''color'',''k'') ]'];
s4 =['[text(.5,.15,''Slope''' tp ')' ...
' line([.3 .3],[.4 .95],''color'',''k'') ' ...
' line([.7 .7],[.4 .95],''linestyle'',''--'',''color'',''k'')' ...
' line([.1 .9],[.35 .95],''linestyle'',''--'',''color'',''k'')' ...
' line([.3 .7],[.5 .8],''linestyle'',''none'',''marker'',''o'','...
'''color'',''k'') ]'];
r_iconstr = str2mat(s1,s2,s3,s4);
c1 = 'sbswitch(''ruler'',''newtype'',''vertical'')';
c2 = 'sbswitch(''ruler'',''newtype'',''horizontal'')';
c3 = 'sbswitch(''ruler'',''newtype'',''track'')';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -