matlab句柄图形对象

发布时间:2016-09-12 20:21:04   来源:文档文库   
字号:

句柄图形对象

句柄图形对象创建函数

Figure对象操作函数

坐标轴控制相关属性

颜色相关的属性

Axes对象绘图操作命令

(1)

function my_plot

x=1:10

y=peaks(10)

%Z = peaks(n);returns an n-by-n matrix.

cax=newplot

%newplot prepares a figure and axes for subsequent graphics commands.

%h = newplot prepares a figure and axes for subsequent graphics commands and returns a handle to the current axes.

LSO=['- ';'--';': ';'-.']

%此处第一项与第三项要加空格,使得各行元素相等

set(cax,'FontName','Times','FontAngle','italic')

%设置图形坐标轴的字体与字体倾斜角度

set(get(cax,'Parent'),'MenuBar','none')%关闭工具栏

line_handles=line(x,y,'Color','b')

%将10条曲线利用直线全部画出,并返回直线句柄向量,10条曲线对应10个句柄值

%所以该句柄向量一共有10个元素

style=1

for i=1:length(line_handles)

if style>length(LSO)

style=1

end

set(line_handles(i),'LineStyle',LSO(style,:))

%设置直线属性

style=style+1

end

grid on

(2)

function my_plot3(x,y,z)

cax=newplot

hold_state=ishold

%检测当前的hold状态

LSO=['- ';'--';': ';'-.']

nargin

if nargin==2

%nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。

%通常可以用他来设定一些默认值,如下面的函数。例子,函数test1的功能是输出a和b的和。

%如果只输入一个变量,则认为另一个变量为0,如果两个变量都没有输入,则默认两者均为0。

%function y=test1(a,b)

%if nargin==0

%a=0;b=0;

%elseif nargin==1

%b=0;

%end

y=a+b;

hlines=line(x,y,'Color','k')

if ~hold_state

%如果hold为off时,改变视图

view(2)

end

elseif nargin==3

hlines=line(x,y,z,'Color','k')

if ~hold_state

%如果hold为off时,改变视图

view(3)

end

end

ls=1

for hindex=1:length(hlines)

if ls>length(LSO)

ls=1

end

set(hlines(hindex),'LineStyle',LSO(ls,:))

ls=ls+1

end

(3)

function visit_handle

x=0:15

y=[1.5*cos(x);4*exp(-0.1*x).*cos(x);exp(0.05*x).*cos(x)]

h=stem(x,y')

%注意转换成列向量

axis([0,16,-4,4])

set(h(1),'Color','black','Marker','o','Tag','Decaying Exponentail')

%h(1)表示Y第一行元素在图形中的属性

set(h(2),'Color','black','Marker','square','Tag','Growing Exponentail')

%Marker表示元素点表示方法,列标识不同

set(h(3),'Color','black','Marker','*','Tag','Steady State ')

%图形对象有句柄,是一个数;GUI里的控件有tag,是一个字符串。都是独一无二的标识。

%h = plot(sin(0:.01:2*pi);返回的h是曲线的句柄,它没有tag 比如某个文本的tag值为t1,设置其内容为hello,则可以这么写set(handles.t1,'string','hello');

set(findobj(gca,'-depth',1,'Type','line'),'LineStytle','--')%将图形中所有属性为直线的对象变为虚线

h=findobj('-regexp','Tag','^(?!Steady State$).')

%返回所有正则表达式Tag为Steady State属性的对象

set(h,{'MarkerSize'},num2cell(cell2mat(get(h,'MarkerSize'))+2))

%改变上述属性对象的标记大小

h=findobj('type','line','Marker','none','-and','-not','LineStyle','--')

%返回线性为直线没有标记并且线性不是虚线的对象

set(h,'Color','red')

%改变上述对象的颜色

(4)

function setup_axes

h=axes('Color',[0.9,0.9,0.9],...

'GridLineStyle','--',...

'ZTickLabel','-1|Z=0 Plane|+1',...

'FontName','times',...

'FontAngle','italic',...

'FontSize',14,...

'XColor',[0 0 0.7],...

'YColor',[0 0 0.7],...

'ZColor',[0 0 0.7])

set(get(h,'XLabel'),'String','Value of X')

set(get(h,'YLabel'),'String','Value of Y')

set(get(h,'Title'),'String','\fontname{times}\itZ=f(x,y)')

%设置标题Z=f(x,y)的字形

set(get(h,'XLabel'),'String','Value of X',...

'FontName','times',...

'FontAngle','italic',...

'FontSize',14)

(5)

function onefigure_severeaxes

%在坐标轴外放置文本

h=axes('Position',[0 0 1 1],'Visible','off')

%对该坐标轴不可见

axes('Position',[0.25 0.1 0.7 0.8])

t=0:900

plot(t,0.25*exp(-0.005*t))

str(1)={'Plot of the function:'}

str(2)={' y=A{\ite}^{-\alpha{\itt}} '}

%注意字符串书写格式

str(3)={'With the values:'}

str(3)={' A=0.25'}

str(4)={' \alpha=0.005 '}

%特殊字符前加\

str(5)={' t=0:900 '}

set(gcf,'CurrentAxes',h)

text(0.025,0.6,str,'FontSize',12)

%在同一个图形中显示不同缩放尺度的图形

h(1)=axes('Position',[0 0 1 1])

sphere

h(2)=axes('Position',[0 0 0.4 0.6])

sphere

h(3)=axes('Position',[0 0.5 0.5 0.5])

sphere

h(4)=axes('Position',[0.5 0 0.4 0.4])

sphere

h(5)=axes('Position',[0.5 0.5 0.5 0.3])

sphere

set(h,'Visible','off')

%显示双坐标轴

x1=[0:0.1:40]

y1=4.*cos(x1)./(x1+2)

x2=[1:0.2:20]

y2=x2.^2./x2.^3

%显示第一个坐标轴对象

h11=line(x1,y1,'Color','r')

ax1=gca

set(ax1,'XColor','r','YColor','r')

%添加第二个坐标轴显示对象

ax2=axes('Position',get(ax1,'Position'),...

'XAxisLocation','top',...

'YAxisLocation','right',...

'Color','none',...

'XColor','k','YColor','k')

h12=line(x2,y2,'Color','k','Parent',ax2)

xlimist1=get(ax1,'Xlim')

ylimist1=get(ax1,'Ylim')

xlinc1=(xlimist1(2)-xlimist1(1))/5

ylinc1=(ylimist1(2)-ylimist1(1))/5

xlimist2=get(ax2,'Xlim')

ylimist2=get(ax2,'Ylim')

xlinc2=(xlimist2(2)-xlimist2(1))/5

ylinc2=(ylimist2(2)-ylimist2(1))/5

%设置标度显示

set(ax1,'XTick',[xlimist1(1):xlinc1:xlimist1(2)],...

'YTick',[ylimist1(1):ylinc1:ylimist1(2)])

set(ax2,'XTick',[xlimist2(1):xlinc2:xlimist2(2)],...

'YTick',[ylimist2(1):ylinc2:ylimist2(2)])

grid on

(6)

t=0:pi/20:2*pi

s=sin(t)

c=cos(t)

%%设置axes对象的Color属性

figh=figure('Position',[30 100 800 350],...

'DefaultAxesColor',[0.8 0.8 0.8])

%%position [left bottom width height],set(gca,'Units')

%%[inches | centimeters | points | pixels]

axh1=subplot(1,2,1)

grid on

%%设置第一个Axes对象的LineStyle属性

set(axh1,'DefaultLineLineStyle','-.')

line('XData',t,'YData',s)

line('XData',t,'YData',c)

text('Position',[3 0.4],'String','Sine')

text('Position',[2 -0.3],'String','Cosine',...

'HorizontalAlignment','right') %文字水平对齐

axh2=subplot(1,2,2)

grid on

%%设置第二个Axes对象的文字旋转属性

set(axh2,'DefaultTextRotation',90)

%文字旋转90度

line('XData',t,'YData',s)

line('XData',t,'YData',c)

text('Position',[3 0.4],'String','Sine')

text('Position',[2,0.3],'String','Cosine',...

'HorizontalAlignment','right')

Z=peaks

plot(1:49,Z(4:7,:))

close

set(0,'DefaultAxesColorOrder',[0 0 0],...

'DefaultAxesLineStyleOrder','-|--|:|-.')

%设置属性默认值,坐标轴颜色顺序,曲线线性顺序

plot(1:49,Z(4:7,:))

set(0,'DefaultAxesColorOrder','remove',...

'DefaultAxesLineStyleOrder','remove')

%删除属性默认值

set(0,'DefaultSurfaceEdgeColor','k')

h=surface(peaks)

set(gcf,DefaultSurfaceEdgeColor','g')

set(h,'EdgeColor')

(7)

function copy_detele_obj

x=0:0.01:6.28

y=sin(x)

figure

plot(x,y)

text_handle=text('String','\{5\pi\div4,sin(5\pi\div4)\}\rightarrow',...

'Position',[5*pi/4,sin(5*pi/4),0],'HorizontalAlignment','right')

%创建text对象句柄,将该句柄复制到其他图形中。

x1=0.5:0.01:6.78

y1=sin(x1)

figure

plot(x1,y1)

copyobj(text_handle,gca)

%删除图形句柄对象,删除y2第一列与第二列的曲线

x2=0:0.05:50

y2=[1.5*cos(x2);4*exp(-0.1*x2).*cos(x2);exp(0.05*x2).*cos(x2)]'

figure

h=plot(x2,y2)

delete(h(1:2))

2016/8/21

ax = get(gcf,'Children');

set(ax(1), 'xlim', [4085, 4100]);

输出图像进行命令行缩放功能

本文来源:https://www.2haoxitong.net/k/doc/5e9beb0df61fb7360a4c6550.html

《matlab句柄图形对象.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式