如何在3个心电图axes 72中显示3个图形

TOP热门关键词
专题页面精选
来自:[url=].cn/s/blog_4aee288a0100967g.html[/url]和/bigheadd/blog/item/ff61d377abe0b.html,谢谢!helpplothelpaxisa1=plot();hlodona2=plot();legend([a1 ...
来自:[url=].cn/s/blog_4aee288a0100967g.html[/url]和/bigheadd/blog/item/ff61d377abe0b.html,谢谢!
a1=plot();
a2=plot();
legend([a1 a2],'图1名',‘图2名')
x1=-pi:pi/12:
x2=-pi:pi/12:
y1=sin(x1);
y2=cos(x2);
plot(x1,y1,x2,y2);
axis([-2*pi 2*pi -2 2]);
xlabel('x');
ylabel('y');
title('sin(x) & cos(x)');
MATLAB受到控制界广泛接受的一个重要原因是因为它提供了方便的绘图功能.这里主要介绍2维图形对象的生成函数及图形控制函数的使用方法,还将简单地介绍一些图形的修饰与标注函数及操作和控制MATLAB各种图形对象的方法.
第一节 图形窗口与坐标系
一.图形窗口
1.MATLAB在图形窗口中绘制或输出图形,因此图形窗口就像一张绘图纸.
2.在MATLAB下,每一个图形窗口有唯一的一个序号h,称为该图形窗口的句柄.MATLAB通过管理图形窗口的句柄来管理图形窗口;
3.当前窗口句柄可以由MATLAB函数gcf获得;
4.在任何时刻,只有唯一的一个窗口是当前的图形窗口(活跃窗口);figure(h)----将句柄为h的窗口设置为当前窗口;
5.打开图形窗口的方法有三种:
1)调用绘图函数时自动打开;
2)用File---New---Figure新建;
3)figure命令打开,close命令关闭.
在运行绘图程序前若已打开图形窗口,则绘图函数不再打开,而直接利用已打开的图形窗口;若运行程序前已存在多个图形窗口,并且没有指定哪个窗口为当前窗口时,则以最后使用过的窗口为当前窗口输出图形.
6.窗口中的图形打印:用图形窗口的File菜单中的Print项.
7.可以在图形窗口中设置图形对象的参数.具体方法是在图形窗口的Edit菜单中选择Properties项,打开图形对象的参数设置窗口,可以设置对象的属性.
1.一个图形必须有其定位系统,即坐标系;
2.在一个图形窗口中可以有多个坐标系,但只有一个当前的坐标系;
3.每个坐标系都有唯一的标识符,即句柄值;
4.当前坐标系句柄可以由MATLAB函数gca获得;
5.使某个句柄标识的坐标系成为当前坐标系,可用如下函数:axes(h) h为指定坐标系句柄值.
6.一些有关坐标轴的函数:
1)定义坐标范围:一般MATLAB自动定义坐标范围,如用户认为设定的不合适,可用:axis([Xmin, Xmax, Ymin, Ymax])重新设定;
2)坐标轴控制:MATLAB的缺省方式是在绘图时,将所在的坐标系也画出来,为隐去坐标系,可用axis on则显示坐标轴(缺省值).
3)通常MATLAB的坐标系是长方形,长宽比例大约是4:3,为了得到一个正方形的坐标系可用:axis square
4)坐标系横纵轴的比例是自动设置的,比例可能不一样,要得到相同比例的坐标系,可用:axis equal
第二节 二维图形的绘制
一. plot函数
plot函数是最基本的绘图函数,其基本的调用格式为:
1.plot(y)------绘制向量y对应于其元素序数的二维曲线图,如果y为复数向量,则绘制虚部对于实部的二维曲线图.
例:绘制单矢量曲线图.
y=[0 0.6 2.3 5 8.3 11.7 15 17.7 19.4 20];
由于y矢量有10个元素,x坐标自动定义为[1 2 3 4 5 6 7 8 9 10].
2.plot(x,y)------绘制由x,y所确定的曲线.
1)x,y是两组向量,且它们的长度相等,则plot(x,y)可以直观地绘出以x为横坐标,y为纵坐标的图形.
如:画正弦曲线:
t=0:0.1:2*
2)当plot(x,y)中,x是向量,y是矩阵时,则绘制y矩阵中各行或列对应于向量x的曲线.如果y阵中行的长度与x向量的 长度相同,则以y的行数据作为一组绘图数据;如果y阵中列的长度与x向量的 长度相同,则以y的列数据作为一组绘图数据;如果y阵中行,列均与x向量的长度相 同,则以y的每列数据作为一组绘图数据.
例:下面的程序可同时绘出三条曲线.MATLAB在绘制多条曲线时,会按照一定的规律自动变化每条曲线的的颜色.
x=0:pi/50:2*
y(1,:)=sin(x);
y(2,:)=0.6*sin(x);
y(2,:)=0.3*sin(x);
或者还可以这样用:
x=0:pi/50:2*
y=[ sin(x); 0.6*sin(x); 0.3*sin(x)];
3) 如果x,y是同样大小的矩阵,则plot(x,y)绘出y中各列相应于x中各列的图形.
例:x(1,:)=0:pi/50:2*
x(2,:)=pi/4:pi/50:2*pi+pi/4;
x(3,:)=pi/2:pi/50:2*pi+pi/2;
y(1,:)=sin(x(1,:));
y(2,:)=0.6*sin(x(2,:));
y(3,:)=0.3*sin(x(3,:));
在这个例子中,x------3x101,y------3x101,所以第一个plot按列画出101条曲线,每条3个点;而x'------101x3,y'------101x3,所以第二个plot按列画出3条曲线,每条101个点.
3.多组变量绘图:plot(x1, y1, 选项1, x2, y2, 选项2, ……)上面的plot格式中,选项是指为了区分多条画出曲线的颜色,线型及标记点而设定的曲线的属性.MATLAB在多组变量绘图时,可将曲线以不同的颜色,不同的线型及标记点表示出来.这些选项如下表所示:各种颜色属性选项
'r' 红色 'm' 粉红
'g' 绿色 'c' 青色
'b' 兰色 'w' 白色
'y' 黄色 'k' 黑色
各种线型属性选项
'-' 实线 '--' 虚线
':' 点线 '-.' 点划线
各种标记点属性选项
'.' 用点号绘制各数据点 '^' 用上三角绘制各数据点
'+' 用'+'号绘制各数据点 'v' 用下三角绘制各数据点
'*' 用'*'号绘制各数据点 '&' 用右三角绘制各数据点
' .' 用'.'号绘制各数据点 '&' 用左三角绘制各数据点
's'或squar 用正方形绘制各数据点'p' 用五角星绘制各数据点
'd'或diamond用菱形绘制各数据点 'h' 用六角星绘制各数据点
这些选项可以连在一起用,如:'-.g'表示绘制绿色的点划线,'g+'表示用绿色的'+'号绘制曲线.
1)表示属性的符号必须放在同一个字符串中;
2)可同时指定2~3个属性;
3)与先后顺序无关;
4)指定的属性中,同一种属性不能有两个以上.
例:t=0:0.1:2*
y1=sin(t);
y2=cos(t);
y3=sin(t).*cos(t);
plot(t,y1, '-r',t,y2, ':g',t,y3, '*b')
该程序还可以按下面的方式写:
t=0:0.1:2*
y1=sin(t);
y2=cos(t);
y3=sin(t).*cos(t);
plot(t,y1, '-r')
plot(t,y2, ':g')
plot(t,y3, '*b')
注:在MATLAB中,如画图前已有打开的图形窗口,则再画图系统将自动擦掉坐标系中已有的图形对象,但设置了hold on后,可以保持坐标系中已绘出的图形.
还可以进一步设置包括线的宽度(LineWidth),标记点的边缘颜色 (MarkerEdgeColor),填充颜色(MarkerFaceColor)及标记点的大小(MarkerSize)等其它绘图属性.
例:设置绘图线的线型,颜色,宽度,标记点的颜色及大小.
t=0:pi/20:
y=sin(4*t).*sin(t)/2;
plot(t,y,'-bs','LineWidth',2,... %设置线的宽度为2
'MarkerEdgeColor','k',... %设置标记点边缘颜色为黑色
'MarkerFaceColor','y',... %设置标记点填充颜色为黄色
'MarkerSize',10) %设置标记点的尺寸为10
4.双Y轴绘图:plotyy()函数.
其调用格式为: plotyy(x1,y1,x2,y2)------绘制由x1,y1和x2,y2确定的两组曲线,其中x1,y1的坐标轴在图形窗口的左侧,x2,y2的坐标轴在图形窗口的右侧.
Plotyy(x1,y1,x2,y2, 'function1','function2')------功能同上,function是指那些
绘图函数如:plot,semilogx,loglog等.
例如:在一个图形窗口中绘制双Y轴曲线.
x=0:0.3:12;
y=exp(-0.3*x).*sin(x)+0.5;
plotyy(x,y,x,y,'plot','stem')
stem:绘制stem形式的曲线(上端带圈的竖线).
绘图结果:两条图线自动用不同的颜色区分,两个坐标的颜色与图线的颜色相对应,左边的Y轴坐标对应的是plot形式的曲线,右边的Y坐标对应的是stem形式的曲线.
二.对数坐标图绘制函数:
在对数坐标图的绘制中,有三种绘图函数:semilogx,semilogy和loglog函数.
1)semilogx( )------绘制以X轴为对数坐标轴的对数坐标图.
其调用格式为:semilogx(x,y,'属性选项')其中属性选项同plot函数.该函数只对横坐标进行对数变换,纵坐标仍为线性坐标.
2)semilogy( )------绘制以Y轴为对数坐标轴的对数坐标图.
其调用格式为:semilogy(x,y,'属性选项')该函数只对纵坐标进行对数变换,横坐标仍为线性坐标.
3)loglog( )------ 绘制X,Y轴均为对数坐标轴的图形.
其调用格式为:loglog(x,y,'属性选项')该函数分别对横,纵坐标都进行对数变换.
x=0:0.1:6*
y=cos(x/3)+1/9;
subplot(221), semilogx(x,y);
subplot(222), semilogy(x,y);
subplot(223), loglog(x,y);
4)MATLAB还提供了一个实用的函数:logspace( )函数,可按对数等间距地分布来产生一个向量,其调用格式为:
x=logspace(x1,x2,n)
这里,x1表示向量的起点;x2表示向量的终点;n表示需要产生向量点的个数(一般可以不给出,采用默认值50).在控制系统分析中一般采用这种方法来构成频率向量w.关于它的应用后面还要讲到.
三.极坐标图的绘制函数:
绘极坐标图可用polar( )函数.其调用格式如下:
polar(theta, rho,'属性选项')------theta:角度向量,rho:幅值向量,属性内容与plot函数基本一致.
例如:极坐标模型为:3145/)/)cos((+ =θρ, ],[πθ80∈则绘出极坐标图的程序为:theta=0:0.1:8*
p=cos((5*theta)/4)+1/3;
polar(theta,p)
四.绘制多个子图:subplot( )函数
MATLAB允许在一个图形窗口上绘制多个子图(如对于多变量系统的输
出),允许将窗口分成nxm个部分.
分割图形窗口用subplot函数来实现,其调用格式为:
subplot(n,m,k)或subplot(nmk)------n,m分别表示将窗口分割的行数和列数,k表示要画图部分的代号,表示第几个图形,nmk三个数可以连写,中间不用符号分开.
例如:将窗口划分成2x2=4个部分,可以这样写:
subplot(2,2,1),plot(……)
subplot(2,2,2),……
subplot(2,2,3),……
subplot(2,2,4),……
注:subplot函数没有画图功能,只是将窗口分割.
本文关键词: 本文论坛网址:
您可能感兴趣的文章
本站推荐的文章
本文标题:
本文链接网址:
1.凡人大经济论坛-经管之家转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。第8讲 图形用户界面设计(2)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
喜欢此文档的还喜欢
第8讲 图形用户界面设计(2)
M​a​t​l​a​b​程​序​设​计​中​的​图​形​用​户​界​面​设​计​课​件
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大尺寸(630*500pix)
大小:1.03MB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢给大家一个非常好用的matlab程序(一个figure中画多幅图,colormap如何设置)
function freezeColors(varargin)
% freezeColors& Lock colors of plot, enabling
multiple colormaps per figure. (v2.3)
%&& Problem: There is only one
colormap per figure. This function provides
an easy solution when plots using different colomaps are
in the same figure.
%&& freezeColors freezes the
colors of graphics objects in the current axis so
that subsequent changes to the colormap (or caxis) will not change
colors of these objects. freezeColors works on any graphics
with CData in indexed-color mode: surfaces, images,
scattergroups,
bargroups, patches, etc. It works by converting CData to true-color
based on the colormap active at the time freezeColors is
%&& The original indexed color
data is saved, and can be restored using
unfreezeColors, making the plot once again subject to the colormap
%&& Usage:
freezeColors&&&&&&&
applies to all objects in current axis (gca),
freezeColors(axh)&& same, but
works on axis axh.
%&& Example:
subplot(2,1,1); imagesc(X); freezeColors
subplot(2,1,2); imagesc(Y); freezeColors etc...
Note: colorbars must also be frozen. Due to Matlab 'improvements'
longer be done with freezeColors. Instead, please
the function CBFREEZE by Carlos Adrian Vargas Aguilera
can be downloaded from the MATLAB File Exchange
h= cbfreeze(h), or simply cbfreeze(colorbar)
For additional examples, see test/test_main.m
%&& Side effect on render mode:
freezeColors does not work with the painters
renderer, because Matlab doesn't support rgb color data in
painters mode. If the current renderer is painters,
freezeColors
changes it to zbuffer. This may have unexpected effects on other
of your plots.
See also unfreezeColors, freezeColors_pub.html, cbfreeze.
%&& John Iversen () 3/23/05
%&& Changes:
%&& JRI ()
4/19/06&& Correctly handles
scaled integer cdata
9/1/06&& should now handle all
objects with cdata: images, surfaces,
%&&&&&&&&&&&&&&&
scatterplots. (v 2.1)
%&& JRI 11/11/06 Preserves NaN
colors. Hidden option (v 2.2, not uploaded)
%&& JRI 3/17/07&
Preserve caxis after freezing--maintains colorbar scale (v
%&& JRI 4/12/07&
Check for painters mode as Matlab doesn't support rgb in it.
4/9/08&& Fix preserving caxis for
objects within hggroups (e.g. contourf)
4/7/10&& Change documentation for
% Hidden option for NaN colors:
%&& Missing data are often
represented by NaN in the indexed color
%&& data, which renders
transparently. This transparency will be preserved
%&& when freezing colors. If
instead you wish such gaps to be filled with
%&& a real color, add
'nancolor',[r g b] to the end of the arguments. E.g.
%&& freezeColors('nancolor',[r g
b]) or freezeColors(axh,'nancolor',[r g b]),
%&& where [r g b] is a color
vector. This works on images & pcolor, but not
%&& surfaces.
%&& Thanks to Fabiano Busdraghi
and Jody Klymak for the suggestions. Bugfixes
%&& attributed in the code.
% Free for all uses, but please retain the following:
%&& Original Author:
%&& John Iversen, 2005-10
appdatacode = 'JRI__freezeColorsData';
[h, nancolor] = checkArgs(varargin);
%gather all children with scaled or indexed CData
cdatah = getCDataHandles(h);
%current colormap
nColors = size(cmap,1);
% convert object color indexes into colormap to true-color data
%& current colormap
for hh = cdatah',
&&& %preserve
parent axis clim
&&& parentAx =
getParentAxes(hh);
&&& originalClim
= get(parentAx,
'clim');&&&
%&& Note: Special handling of
patches: For some reason, setting
%&& cdata on patches created by
bar() yields an error,
%&& so instead we'll set
facevertexcdata instead for patches.
~strcmp(g.Type,'patch'),
cdata = g.CD
cdata = g.FaceVertexCD
&&& %get cdata
mapping (most objects (except scattergroup) have it)
isfield(g,'CDataMapping'),
scalemode = g.CDataM
scalemode = 'scaled';
original indexed data for use with unfreezeColors
size(cdata);
setappdata(hh, appdatacode, {cdata scalemode});
&&& %convert
cdata to indexes into colormap
strcmp(scalemode,'scaled'),
%4/19/06 JRI, Accommodate scaled display of integer cdata:
in MATLAB, uint * double = uint, so must coerce cdata to
Thanks to O Yamashita for pointing this need out
idx = ceil( (double(cdata) - cax(1)) / (cax(2)-cax(1)) *
&&& else %direct
/8/09 in case direct data is non-int (e.g.
freezeColors)
% (Floor mimics how matlab converts data into colormap
% Thanks to D Armyr for the catch
idx = floor(idx);
&&& %clamp to
[1, nColors]
idx(idx&1) = 1;
idx(idx&nColors) = nC
&&& %handle
nans in idx
&&& nanmask =
isnan(idx);
idx(nanmask)=1; %temporarily replace w/ a valid colormap index
true-color data--using current colormap
&&& realcolor =
zeros(siz);
&&& for i =
c = cmap(idx,i);
c = reshape(c,siz);
c(nanmask) = nancolor(i); %restore Nan (or nancolor if
specified)
realcolor(:,:,i) =
&&& %apply new
true-color color data
&&& %true-color
is not supported in painters renderer, so switch out of that
strcmp(get(gcf,'renderer'), 'painters'),
set(gcf,'renderer','zbuffer');
&&& %replace
original CData with true-color data
~strcmp(g.Type,'patch'),
set(hh,'CData',realcolor);
set(hh,'faceVertexCData',permute(realcolor,[1 3 2]))
&&& %restore
clim (so colorbar will show correct limits)
~isempty(parentAx),
set(parentAx,'clim',originalClim)
end %loop on indexed-color objects
============================================================================
% Local functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% getCDataHandles -- get handles of all descendents with indexed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hout = getCDataHandles(h)
% getCDataHandles& Find all objects with indexed
%recursively descend object tree, finding objects with indexed
% An exception: don't include children of objects that themselves
have CData:
%&& for example, scattergroups
are non-standard hggroups, with CData. Changing
%&& such a group's CData
automatically changes the CData of its children,
%&& (as well as the children's
handles), so there's no need to act on them.
error(nargchk(1,1,nargin,'struct'))
hout = [];
if isempty(h),end
ch = get(h,'children');
for hh = ch'
isfield(g,'CData'),&&&&
%does object have CData?
%is it indexed/scaled?
if ~isempty(g.CData) &&
isnumeric(g.CData) &&
size(g.CData,3)==1,
&&&&&&&&&&&
hout = [ hh]; %#ok&AGROW& %yes,
add to list
&&& else %no
CData, see if object has any interesting children
&&&&&&&&&&&
hout = [ getCDataHandles(hh)];
%#ok&AGROW&
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% getParentAxes -- return handle of axes object to which a given
object belongs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hAx = getParentAxes(h)
% getParentAxes& Return enclosing axes of a given
object (could be self)
error(nargchk(1,1,nargin,'struct'))
%object itself may be an axis
if strcmp(get(h,'type'),'axes'),
&&& return
parent = get(h,'parent');
if (strcmp(get(parent,'type'), 'axes')),
getParentAxes(parent);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% checkArgs -- Validate input arguments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [h, nancolor] = checkArgs(args)
% checkArgs& Validate input arguments to
freezeColors
nargs = length(args);
error(nargchk(0,3,nargs,'struct'))
%grab handle from first argument if we have an odd number of
if mod(nargs,2),
~ishandle(h),
error('JRI:freezeColors:checkArgs:invalidHandle',...
&&&&&&&&&&&
'The first argument must be a valid graphics handle (to an
&&& % 4/2010
check if object to be frozen is a colorbar
strcmp(get(h,'Tag'),'Colorbar'),
if ~exist('cbfreeze.m'),
warning('JRI:freezeColors:checkArgs:cannotFreezeColorbar',...
&&&&&&&&&&&
['You seem to be attempting to freeze a colorbar. This no
longer'...
&&&&&&&&&&&
'works. Please read the help for freezeColors for the
solution.'])
cbfreeze(h);
&&& args{1} =
&&& nargs =
%set nancolor if that option was specified
nancolor = [nan nan nan];
if nargs == 2,
strcmpi(args{end-1},'nancolor'),
nancolor = args{end};
if ~all(size(nancolor)==[1 3]),
&&&&&&&&&&&
error('JRI:freezeColors:checkArgs:badColorArgument',...
&&&&&&&&&&&&&&&
'nancolor must be [r g b] vector');
nancolor(nancolor&1) = 1;
nancolor(nancolor&0) = 0;
error('JRI:freezeColors:checkArgs:unrecognizedOption',...
&&&&&&&&&&&
'Unrecognized option (%s). Only ''nancolor'' is
valid.',args{end-1})
function CBH = cbfreeze(varargin)
�FREEZE&& Freezes the colormap of
a colorbar.
%&& SYNTAX:
%&&&&&&&&&&
%&&&&&&&&&&
cbfreeze('off')
%&&&&&&&&&&
cbfreeze(H,...)
CBH = cbfreeze(...);
%&& INPUT:
- Handles of colorbars to be freezed, or from figures to
%&&&&&&&&&&&&
for them or from peer axes (see COLORBAR).
%&&&&&&&&&&&&
DEFAULT: gcf (freezes all colorbars from the current figure)
'off' - Unfreezes the colorbars, other options are:
%&&&&&&&&&&&&&&
%&&&&&&&&&&&&&&
'un'&&& same as
%&&&&&&&&&&&&&&
'del'&& Deletes the
colormap(s).
%&&&&&&&&&&&&
DEFAULT: 'on' (of course)
%&& OUTPUT (all optional):
CBH - Color bar handle(s).
%&& DESCRIPTION:
MATLAB works with a unique COLORMAP by figure which is a big
limitation. Function FREEZECOLORS by John Iversen allows to
different COLORMAPs in a single figure, but it fails freezing
COLORBAR. This program handles this problem.
* Optional inputs use its DEFAULT value when not given or [].
* Optional outputs may or not be called.
* If no colorbar is found, one is created.
* The new frozen colorbar is an axes object and does not
as normally colorbars when resizing the peer axes. Although,
time the normal behavior is not that good.
* Besides, it does not have the 'Location' property anymore.
* But, it does acts normally: no ZOOM, no PAN, no ROTATE3D and
mouse selectable.
* No need to say that CAXIS and COLORMAP must be defined before
this function. Besides, the colorbar location. Anyway, 'off'
'del' may help.
* The 'del' functionality may be used whether or not the
colorbar(s)
is(are) froozen. The peer axes are resized back. Try:
&& colorbar, cbfreeze del
%&& EXAMPLE:
surf(peaks(30))
colormap jet
colormap gray
title('What...?')
%&& SEE ALSO:
COLORMAP, COLORBAR, CAXIS
FREEZECOLORS by John Iversen
MFILE:&& cbfreeze.m
%&& VERSION: 1.1 (Sep 02, 2009)
href="matlab:web('/matlabcentral/fileexchange/authors/11258')"&download&/a&)
%&& MATLAB:&
7.7.0.471 (R2008b)
%&& AUTHOR:&
Carlos Adrian Vargas Aguilera (MEXICO)
%&& CONTACT:
%&& REVISIONS:
Released. (Jun 08, 2009)
Fixed BUG with image handle on MATLAB R2009a. Thanks to
%&&&&&&&&&&&
Muniz. (Sep 02, 2009)
%&& DISCLAIMER:
%&& cbfreeze.m is provided "as
is" without warranty of any kind, under the
%&& revised BSD license.
%&& Copyright (c) 2009 Carlos
Adrian Vargas Aguilera
% INPUTS CHECK-IN
-------------------------------------------------------------------------
% Parameters:
cbappname =
'Frozen';&&&&&&&&
% Colorbar application data with fields:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
% 'Location' from colorbar
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
% 'Position' from peer axes befor colorbar
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
'pax'&&&&&
handle from peer axes.
axappname = 'FrozenColorbar'; % Peer axes application data with
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
% colorbar handle.
% Set defaults:
'on';&&&&&&&&&&&&&&&&&&
Sopt = {'on','un','off','del'};
H = get(0,'CurrentFig');
% Check inputs:
if nargin==2 &&
(~isempty(varargin{1}) &&
all(ishandle(varargin{1})) &&
& isempty(varargin{2}))
&% Check for CallBacks functionalities:
&% ------------------------------------
&varargin{1} = double(varargin{1});
strcmp(get(varargin{1},'BeingDelete'),'on')
& % Working as DeletFcn:
& if (ishandle(get(varargin{1},'Parent'))
~strcmpi(get(get(varargin{1},'Parent'),'BeingDeleted'),'on'))
&&& % The handle
input is being deleted so do the colorbar:
~isempty(getappdata(varargin{1},cbappname))
&&& % The frozen
colorbar is being deleted:
varargin{1};
&&& % The peer
axes is being deleted:
ancestor(varargin{1},{'figure','uipanel'});
&& % The figure is getting
&elseif (gca==varargin{1}
&&&&&&&&&&&&&&&&&&&&
gcf==ancestor(varargin{1},{'figure','uipanel'}))
& % Working as ButtonDownFcn:
& cbfreezedata =
getappdata(varargin{1},cbappname);
& if ~isempty(cbfreezedata)
ishandle(cbfreezedata.ax)
&&& % Turns the
peer axes as current (ignores mouse click-over):
set(gcf,'CurrentAxes',cbfreezedata.ax);
&&& return
&& % Clears application
rmappdata(varargin{1},cbappname)
& H = varargin{1};
&% Checks for normal calling:
&% --------------------------
&% Looks for H:
&if nargin &&
~isempty(varargin{1}) &&
all(ishandle(varargin{1}))
& H = varargin{1};
& varargin(1) = [];
&% Looks for S:
&if ~isempty(varargin)
&& (isempty(varargin{1}) ||
ischar(varargin{1}))
& S = varargin{1};
% Checks S:
if isempty(S)
&S = 'on';
S = lower(S);
iS = strmatch(S,Sopt);
if isempty(iS)
&error('CVARGAS:cbfreeze:IncorrectStringOption',...
& ['Unrecognized ''' S ''' argument.' ])
&S = Sopt{iS};
% Looks for CBH:
CBH = cbfreeze(H); �H = cbhandle(H);
if ~strcmp(S,'del') &&
isempty(CBH)
&% Creates a colorbar and peer axes:
&CBH = colorbar('peer',pax);
&pax = [];
-------------------------------------------------------------------------
-------------------------------------------------------------------------
% Note: only CBH and S are necesary, but I use pax to avoid the use
"hidden" 'Axes' COLORBAR's property. Why... �
% Saves current position:
fig = get(& 0,'CurrentFigure');
cax = get(fig,'CurrentAxes');
% Works on every colorbar:
for icb = 1:length(CBH)
&% Colorbar axes handle:
&h& = double(CBH(icb));
&% This application data:
&cbfreezedata = getappdata(h,cbappname);
&% Gets peer axes:
&if ~isempty(cbfreezedata)
& pax = cbfreezedata.
& if ~ishandle(pax) % just in case
&& rmappdata(h,cbappname)
&& continue
&elseif isempty(pax) % not generated
double(get(h,'Axes'));& % NEW feature in
&& continue
&% Choose functionality:
& case 'del'
&& % Deletes:
~isempty(cbfreezedata)
&&& % Returns
axes to previous size:
&&& oldunits =
get(pax,'Units');
set(pax,'Units','Normalized');
set(pax,'Position',cbfreezedata.Position)
set(pax,'Units',oldunits)
set(pax,'DeleteFcn','')
isappdata(pax,axappname)
rmappdata(pax,axappname)
strcmp(get(h,'BeingDelete'),'off')
& case {'un','off'}
&& % Unfrozes:
~isempty(cbfreezedata)
delete(h);
set(pax,'DeleteFcn','')
isappdata(pax,axappname)
rmappdata(pax,axappname)
&&& oldunits =
get(pax,'Units');
set(pax,'Units','Normalized')
set(pax,'Position',cbfreezedata.Position)
set(pax,'Units',oldunits)
&&& CBH(icb) =
colorbar(...
'Location',cbfreezedata.Location);
& otherwise % 'on'
&& % Freezes:
&& % Gets colorbar axes
properties:
&& cb_prop& =
&& % Gets colorbar image handle.
Fixed BUG, Sep 2009
findobj(h,'Type','image');
&& % Gets image data and
transform it in a RGB:
&& CData = get(hi,'CData');
&& if size(CData,3)~=1
&&& % It's
already frozen:
&& % Gets image tag:
&& Tag = get(hi,'Tag');
&& % Deletes previous colorbar
preserving peer axes position:
&& oldunits =
get(pax,'Units');
&&&&&&&&&&&&&
set(pax,'Units','Normalized')
&& Position =
get(pax,'Position');
&& delete(h)
&& cbfreezedata.Position =
get(pax,'Position');
&&&&&&&&&&&&&
set(pax,'Position',Position)
&&&&&&&&&&&&&
set(pax,'Units',oldunits)
&& % Generates new colorbar
&& % NOTE: this is needed because
each time COLORMAP or CAXIS is used,
MATLAB generates a new COLORBAR! This eliminates that
and is the central point on this function.
&& h = axes(...
'Parent'& ,cb_prop.Parent,...
'Units'&& ,'Normalized',...
'Position',cb_prop.Position...
&& % Save location for future
&& cbfreezedata.Location =
&& % Move ticks because IMAGE
draws centered pixels:
&& XLim = cb_prop.XL
&& YLim = cb_prop.YL
isempty(cb_prop.XTick)
&&& X = XLim(1)
+ diff(XLim)/2;
diff(YLim)/(2*length(CData))*[+1 -1];
&& else % isempty(YTick)
Horizontal:
&&& Y = YLim(1)
+ diff(YLim)/2;
diff(XLim)/(2*length(CData))*[+1 -1];
&& % Draws a new RGB image:
image(X,Y,ind2rgb(CData,colormap),...
'Parent'&&&&&&&&&&&
'HitTest'&&&&&&&&&&
,'off',...
'Interruptible'&&&&
,'off',...
'SelectionHighlight','off',...
'Tag'&&&&&&&&&&&&&&
&& % Removes
'...Mode'&& properties:
&& cb_fields =
fieldnames(cb_prop);
indmode&& =
strfind(cb_fields,'Mode');
&& for k=1:length(indmode)
~isempty(indmode{k})
cb_prop = rmfield(cb_prop,cb_fields{k});
&& % Removes special COLORBARs
properties:
&& cb_prop =
rmfield(cb_prop,{...
'CurrentPoint','TightInset','BeingDeleted','Type',...&&&&&&
% read-only
'Title','XLabel','YLabel','ZLabel','Parent','Children',...&
'UIContextMenu','Location',...&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
% colorbars
'ButtonDownFcn','DeleteFcn',...&&&&&&&&&&&&&&&&&&&&&&&&&&&&
% callbacks
'CameraPosition','CameraTarget','CameraUpVector','CameraViewAngle',...
'PlotBoxAspectRatio','DataAspectRatio','Position',...
'XLim','YLim','ZLim'});
&& % And now, set new axes
properties almost equal to the unfrozen
&& % colorbar:
&& set(h,cb_prop)
&& % CallBack features:
&& set(h,...
'ActivePositionProperty','position',...
'ButtonDownFcn'&&&&&&&&
,@cbfreeze,...& % mhh...
'DeleteFcn'&&&&&&&&&&&&
,@cbfreeze)&&&&
set(pax,'DeleteFcn'&&&&&
,@cbfreeze)&&&&
% and again!&
&& % Do not zoom or pan or
setAllowAxesZoom&
setAllowAxesPan&&
setAllowAxesRotate(rotate3d,h,false)
&& % Updates data:
&& CBH(icb) =
&& % Saves data for future
cbfreezedata.pax&&&&&&
&& setappdata(&
h,cbappname,cbfreezedata);
setappdata(pax,axappname,h);
&end % switch
functionality&&
end& % MAIN loop
% OUTPUTS CHECK-OUT
-------------------------------------------------------------------------
% Output?:
if ~nargout
&clear CBH
&CBH(~ishandle(CBH)) = [];
% Returns current axes:
if ishandle(cax)
&set(fig,'CurrentAxes',cax)
% [EOF]&& cbfreeze.m
大家只要把这个.m文件matlab工作文件夹中即可,此函数用来在一个figure上作出多个立体图形。
subplot(1,2,1)
sphere(10)
freezeColors
subplot(1,2,2)
图形如下:
这样在同一个figure中就显示出两个colorbar。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 matlab guide axes 的文章

 

随机推荐