matlab实现的画中画放大代码(转载)
标签:
matlab画中画教育 |
分类: Matlab学习 |
matlab实现的画中画放大代码
经过几天的修改,终于完成了稍微像样的Matlab程序
http://s9/middle/49d955154a177ac13bcc8&690
function myzoom
%% 综合实例
% 俗话说,一图值千字。同样,对于应用软件而言,一例值千言。
%% 画中画缩放程序
myDraw
while 1
end
%% getn
function varargout=getn(H,varargin)
% 演示了可变参数及可变返回值的使用
% This simplifies the construct
% [Xlim,Ylim,Xlabel] =
deal(get(gca,{'Xlim','Ylim','Xlabel'}))
if max(size(H))~=1 || ~ishandle(H)
end
varargout=get(H,varargin);
%% myDraw
function myDraw
t = linspace(0,1); %等分100份
x = sin(2*pi*t);
y = 1.2*cos(4*pi*t);
z = exp(-2*t).*sin(3*pi*t - pi/4);
plot(t,x,t,y,'-.',t,z,'--')
title('画中画示例.');xlabel 't',ylabel('X, Y, Z')
h=legend('x','y','z',2); %设置图例,0--空地,1--右上,2--左上
%
%% getbox
function [xbox,ybox,prect]=getbox
%获取选择框
Hf =
gcf;
Ha = gca(Hf); % get current axes where button was pressed
AxesPt = get(Ha,'CurrentPoint'); % 返回当前坐标系统下的位置,3D。get first axes
data point clicked
FigPt = get(Hf,'CurrentPoint'); % 返回当前图形坐标下的位置,与用户坐标无关,2D。get first
figure point clicked
rbbox([FigPt 0 0],FigPt) % function returns as soon as mouse button
is up
AxesPt = [AxesPt;get(Ha,'CurrentPoint')];
[Xlim,Ylim] = getn(Ha,'Xlim','Ylim');
xbox = [min(AxesPt(:,1)) max(AxesPt(:,1))]; % x axis limits of
selection
xbox = [max(xbox(1),Xlim(1)) min(xbox(2),Xlim(2))]; % limit to axes
size
ybox = [min(AxesPt(:,2)) max(AxesPt(:,2))];
ybox = [max(ybox(1),Ylim(1)) min(ybox(2),Ylim(2))]; % limit to axes
size
prect = [xbox(1) ybox(1) diff(xbox) diff(ybox)]; % position
rectangle
%% MMZOOM
function mmzoom(xlim,ylim,prect)
Hzoom = findobj(0,'Tag','MMZOOM'); % find previous zoomed
axes
if ~isempty(Hzoom)
end
Hzr =
rectangle('Position',prect,'Linestyle',':','Tag','MMZOOM');
Haxes = gca; % handle of axes where selection box
was drawn
Hzr =
rectangle('Position',prect,'Linestyle',':','Tag','MMZOOM');
Hfig = gcf; % handle of Figure where selection box was drawn
Hzoom = copyobj(Haxes,Hfig); % copy original axes and its
children
Pvect=getNormalSize(Haxes,'Position');
alpha = 1/3;
beta = 98/100; % position shift for zoomed axes
Zwidth =
alpha*Pvect(3);
Zheight =
alpha*Pvect(4);
Zleft =
Pvect(1)+beta*Pvect(3)-Zwidth;
Zbottom = Pvect(2)+beta*Pvect(4)-Zheight; % zoomed axes
bottom
set(Hzoom,'units','Normalized',...
[Htx,Hty,Htt] = getn(Hzoom,'Xlabel','Ylabel','Title');
set([Htx,Hty,Htt],'String','')
% place zoomed axes at top of object stack
Hchild = findobj(Hfig,'type','axes'); % get all axes in
figure
Hchild(Hchild==Hzoom) =
[];
set(Hfig,'Children',[Hzoom;Hchild],'CurrentAxes',Haxes,'ButtonDownFcn','mmzoom
reset')
%% getNormalSize
function [normalValue]=getNormalSize(Ha,prName)
OldUnits = get(Ha,'Units'); % get position vector of original
set(Ha,'Units','normalized') % axes in normalized units
normalValue = get(Ha,prName);
set(Ha,'Units',OldUnits)
来源:http://hi.baidu.com/imhuanxi/blog/item/5d1a7629c6575df499250ad7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
更新
Matlab官网的File Exchange有一个不错的例子
magnify
version 1.0 (4.13 KB) by Rick Hindman
Turns the mouse into a pop-up magnifying glass to look at details
of 2D plots.
Ever wish MATLAB had a magnifying glass so you could look at the details of a small region of your plots without having to zoom in and out again and again? Just run 'magnify.m' with the figure of interest as the current figure, then use the left mouse button to bring up a magnified veiw that you control. Or use the 'Ctrl' key while clicking to bring up a magnifying glass that 'locks' onto the figure when released (for use when copying and printing figures). Use the '<' and '>' keys to make the magnifying glass smaller or larger. Use '-' and '+' to decrease or increase the magnification level.
http://cn.mathworks.com/matlabcentral/fileexchange/5961-magnify
可免费注册mathworks账户,然后下载。

加载中…