文章目录
- 前言:
- 图形对象:
- 图形句柄:
- 图形对象的属性:
- 对象的基本操作
- figure对象(图形窗口)
- figure()函数:
- clf()函数清空当前图形窗口:
- axes坐标轴对象
- image()
- line()
- text()
前言:
下面这些是为了更好地写回调函数。例子我后面会整出来,都是可以在gui里面整的,非常强,先罗列一下基本知识及定义。
图形对象:
Matlab中,把用于数据可视化和界面制作的基本绘图要素称为图形对象。每一个具体的图形都是由若干不同的图形对象构成。
图形句柄:
matlab在创建每一个图形对象时,都为该对象分配唯一的一个值, 称其为图形对象句柄。句柄是图形对象的唯一标识符,不同对象的句柄不可 能重复和混淆
计算机屏幕作为根对象(root)由 系统自动建立,其句柄值为0。而图形窗口对象(figure)的句柄值为一正整数,并显示在该窗口的标题栏。其他图形对象的句柄为浮点数。
MATLAB在创建各种对象时,会产生该对象的句柄,通过句柄可以实现对该对象的各种控制和设置
MATLAB提供了若干个函数用于获取已有图形对象的句柄。
图形对象的属性:
图形对象的属性是 一些特殊值,可以对图形对象进行控制和设置。每个属性都有一个属性名和属性值。
属性名通常是采用大小写字母组成的字符串,第一个字母大写。但是在MATLAB中,图形对象的属性名不区分大小写。
用户可以在创建图形对象时,对属性值进行设置。如果用户没有对属性值进行设置,则所有的属性都会自动初始化为系统的默认值。例如figure(‘Toolbar’,‘none’,‘Menubar’‘none’),将创建没有工具栏和菜单的图形窗口。
图形对象的属性非常多,通常在创建图形对象后,通过句柄对属性值进行修改。利用函数ge()获取图形对象的属性值,通过函数set()设置图形对象的属性值。
对象的基本操作
获取当前图形对象的函数:
函数 | 说明 |
---|---|
gcf() | 获取当前图形窗口的句柄 |
gca() | 获取当前图形窗口中坐标轴的句柄 |
gco() | 获取当前图形窗口中当前对象的句柄 |
gcbf() | 获取正在执行的回调程序对应的对象所在窗口的句柄 |
gcbo() | 获取正在执行的回调程序对应的图像句柄 |
除了get()和set()获取和设置图形对象的属性之外,还可以有其他操作:
函数 | 说明 |
---|---|
reset() | 对象的复位,恢复为默认值 |
findobj() | 对象的查找 |
findall() | 查找所有对象,包括隐藏的对象 |
copyobj() | 对象的复制 |
delete() | 对象的删除 |
allchild() | 查找所有的子对象 |
ancestor() | 查找对象的父对象 |
figure对象(图形窗口)
figure()函数:
Syntax
figure
figure('PropertyName',propertyvalue,...)
figure(h)
h = figure(...)
Description
figure creates a new figure window using default property values. This new figure window becomes the current figure, and it displays on top of all other figures on the screen. The title of the figure is an integer value that is not already used by an existing figure. MATLAB® saves this integer value in the figure’s Number property.
figure(‘PropertyName’,propertyvalue,…) creates a new figure window using specific property values. For a list of available properties, see Figure Properties. MATLAB uses default values for any properties that you do not explicitly define as arguments.
创建属性为PropertyName,值为propertyvalue的图形窗口
figure(h) does one of the following:
If h is the handle or the Number property value of an existing figure, then figure(h) makes that existing figure the current figure, makes it visible, and moves it on top of all other figures on the screen. The current figure is the target for graphics output.
If h is not the handle and is not the Number property value of an existing figure, but is an integer, then figure(h) creates a figure object and assigns its Number property the value h.
If h is not the handle to a figure and is not a positive integer, then MATLAB returns an error.
h = figure(…) returns the handle to the figure object.
clf()函数清空当前图形窗口:
只是清空而不关闭,如果关闭图形窗口采用close().
close all关闭所有
Syntax
clf
clf('reset')
clf(fig)
clf(fig,'reset')
figure_handle = clf(...)
Description
clf
deletes from the current figure all graphics objects whose handles are not hidden (i.e., their HandleVisibility property is set to on).
clf(‘reset’)
deletes from the current figure all graphics objects regardless of the setting of their HandleVisibility property and resets all figure properties except Position, Units, PaperPosition, and PaperUnits to their default values.
clf(fig) or clf(fig,‘reset’)
clears the single figure with handle fig.
清除窗口fig中所有不隐藏的对象
figure_handle = clf(…)
returns the handle of the figure. This is useful when the figure IntegerHandle property is off because the noninteger handle becomes invalid when the reset option is used (i.e., IntegerHandle is reset to on, which is the default).
例子:
figure;
t=-pi:pi/20:pi;
plot(t,cos(t),'r');%绘制曲线
pause(3);
clf(gcf);%清空图形窗口内容
pause(3);
close(gcf);%关闭图形窗口
axes坐标轴对象
Syntax
axes
axes('PropertyName',propertyvalue,...)
axes(parent,...)
axes(h)
h = axes(...)
Description
axes creates an axes graphics object in the current figure using default property values. axes is the low-level function for creating axes graphics objects. MATLAB® automatically creates an axes, if one does not already exist, when you issue a command that creates a graph.
axes(‘PropertyName’,propertyvalue,…) creates an axes object having the specified property values. For a description of the properties, see Axes Properties. MATLAB uses default values for any properties that you do not explicitly define as arguments. The axes function accepts property name/property value pairs, structure arrays, and cell arrays as input arguments (see the set and get commands for examples of how to specify these data types). While the basic purpose of an axes object is to provide a coordinate system for plotted data, axes properties provide considerable control over the way MATLAB displays data.
axes(parent,…) creates the axes in the figure, uipanel, or uitab specified by parent, instead of in the current figure.
axes(h) makes existing axes h the current axes and brings the figure containing it into focus. It also makes h the first axes listed in the figure’s Children property and sets the figure’s CurrentAxes property to h. The current axes is the target for functions that draw image, line, patch, rectangle, surface, and text graphics objects.
If you want to make an axes the current axes without changing the state of the parent figure, set the CurrentAxes property of the figure containing the axes:
set(figure_handle,‘CurrentAxes’,axes_handle)
This command is useful if you want a figure to remain minimized or stacked below other figures, but want to specify the current axes.
h = axes(…) returns the handle of the created axes object.
Use the set function to modify the properties of an existing axes or the get function to query the current values of axes properties. Use the gca command to obtain the handle of the current axes.
The axis (not axes) function provides simplified access to commonly used properties that control the scaling and appearance of axes.
Set default axes properties on the figure and root levels:
set(groot,‘DefaultAxesPropertyName’,PropertyValue,…)
set(gcf,‘DefaultAxesPropertyName’,PropertyValue,…)
PropertyName is the name of the axes property and PropertyValue is the value you are specifying. Use set and get to access axes properties.
利用函数gca可以获取当前坐标轴的句柄,因此,利用get(gca)可获取当前坐标轴的属性。通过函数set(gca,‘PropertyName’,‘PropertyValue’)可以对坐标轴的属性进行设置
例子:
figure;
h=axes;
set(h,'Color',[1 0 0]);
set(h,'Units','Centimeters');
image()
clear all;
close all;
A=imread('C:\Users\***\Desktop\dogg.png','png');
A=im2double(A);
h=image(A);%高级调用
set(gca,'xlim',[0 500],'ylim',[0 400]);
get(h,'type');
clear all;
close all;
A=imread('C:\Users\***\Desktop\dogg.png','png');
A=im2double(A);
h=image('CData',A);%采用CData属性进行图像显示
set(gca,'xlim',[0 500],'ylim',[0 400]);
get(h,'type');
加一个set(gca,‘ydir’,‘reverse’)可将坐标轴y轴反向,从而使两幅图相同
line()
为了方便,我直接做成表格了
Syntax | Description |
---|---|
line | |
line(X,Y) | 绘制二维曲线 |
line(X,Y,Z) | 绘制三维曲线 |
line(X,Y,Z,‘PropertyName’,propertyvalue,…) | 对线条对象的属性进行设置 |
line(‘XData’,x,‘YData’,y,‘ZData’,z,…) | |
line(ax,…) | |
h = line(…) | 返回线条对象的句柄 |
例子:
clear all;
close all;
t=0:pi/20:pi;
x=sin(t);
y=cos(t);
z=sin(t).*cos(t);
h=line(x,y,z);%线条对象
set(h,'LineWidth',2,'color',[1 0 0]);%设置属性值
view(3);%改变视角
set(gca,'xgrid','on','ygrid','on','zgrid','on');%设置网格
set(gcf,'position',[200,200,400,400]);%设置图形窗口位置和大小
此外还可通过LineStyle设置线条类型,Marker设置数据点的标记类型,Markersize设置标记点的大小
结果:
text()
Syntax | Description |
---|---|
text(x,y,str) | 在当前坐标轴中的位置(x,y)显示字符串string |
text(x,y,z,str) | 在三维坐标轴中的位置(x,y,z)显示字符串string |
text(___,Name,Value) | 对文本对象的属性进行设置 |
text(ax,___) | |
t = text(___) | 返回text文本对象的句柄 |
例子:
clear all;
close all;
h1=text(0.3,0.5,'hi,my friend');
set(h1,'Color',[1 0 0]);
h2=text(0.5,0.8,'hi,my friend');
set(h2,'FontSize',14);
set(h2,'BackgroundColor',[0 1 0]);
h3=text(0.6,0.1,'hi,my friend','FontSize',13);
set(h3,'rotation',90);