承接上文,下面学习到GUI程序设计的文字对象。
上文链接https://blog.csdn.net/weixin_42129435/article/details/118637744
1.5 文字对象
使用text函数可以根据指定位置和属性值添加文字说明,并保存句柄。该函数的调用格式为
句柄变量= text(x,y,z,说明文字,属性名1,属性值1,属性名2,属性值2,…)
其中说明文字中除使用标准的ASCII字符外,还可使用LaTeX格式的控制字符。
文字对象的常用属性如下图所示。
例 文字对象标注
text(0.2,0.5,'文字');
例 制作一个简易的水平循环滚动条,显示“12345678”。
clc;clear;close all;
%%初始化滚动条参数
isMoveFirst = true; %值为真时移动文本控件hDown, 值为假时移动文本控件hDown2
delt = 10; %每次移动的长度,单位为points
a =50; %滚动条左边界与窗口左边界的距离
b =50; %滚动条右边界与窗口右边界的距离
width = 450; %窗口的宽度
height = 200; %窗口的高度
strDisp = '12345678'; %要滚动显示的字符串
%%创建隐藏的窗口,并将窗口移到屏幕中间
hFigure = figure( 'Name', '文字对象设计示例','MenuBar', 'none', 'ToolBar', 'none',...
'NumberTitle', 'off', 'Units', 'points', 'Position', [0 0 width height],...
'Visible', 'off');
movegui(hFigure, 'center');
%%设置uicontrol控件默认的字体大小、字体粗细和计量单位
set(0, 'DefaultuicontrolFontSize', 12);
set(0, 'DefaultuicontrolFontWeight', 'bold');
set(0, 'DefaultuicontrolUnits', 'point');
%%以下5个控件创建的顺序不能颠倒
uicontrol( 'Style', 'edit', 'Enable', 'inactive', 'BackgroundColor', 'w', 'Position',...
[a-2 height/2 width-a-b+4 30],'ForegroundColor', 'r'); %创建白色背景
hDown2 = uicontrol( 'Style', 'text', 'BackgroundColor', 'w', 'String', strDisp, 'Position',...
[width-b height/2+1 300 24],'ForegroundColor', 'r', 'Hor', 'left'); %创建文本控件hDown2
hDown = uicontrol( 'Style', 'text', 'BackgroundColor', 'w', 'String', strDisp,...
'Position', [width-b height/2+1 300 24], 'Hor', 'left'); %创建文本控件hDown
hUpLeft = uicontrol( 'Style', 'text', 'Position', [a- 202 height/2 200 30],...
'BackgroundColor', get(hFigure, 'Color')); %创建左边界遮挡条
hUpRight = uicontrol('Style', 'text', 'Position', [width- b- 2 height/2 200 30],...
'BackgroundColor', get( hFigure, 'Color')); %创建右边界遮挡条
%%显示窗口
set(hFigure, 'Visible', 'on');
%%循环显示
while ishandle(hFigure)
if isMoveFirst %isMoveFirst值为真时移动文本控件hDown
pos = get(hDown, 'position');
pos(1) = pos(1) - delt;
if pos(1) + 300>a %若文本控件hDown的最右端在hUpLeft的覆盖范围之外
set(hDown, 'position', pos);
else %若文本控件hDown被hUpLeft完全覆盖
isMoveFirst = false;
pos(1) = width - b;
set(hDown, 'Position', pos);
end
if pos(1)<a %若文本控件hDown的最左端被hUpLeft瘦盖,开始移动文本控件hDown2
pos = get(hDown2, 'position');
pos(1) = pos(1) - delt;
set(hDown2,'Position', pos);
end
else %isMoveFirst值为假时移动文本控件hDown2
pos =get(hDown2, 'position');
pos(1) = pos(1) - delt;
if pos(1) > -300 %若文本控件hDown2的最右端在hUpLeft的覆盖范围之外
set( hDown2, 'position', pos);
else %若文本控件hDown2被hUpLeft完全覆盖
isMoveFirst = true;
pos(1) = width-b;
set( hDown2,'Position', pos);
end
if pos(1) <a2ynrow %若文本控件hDown2的最左端被bUpLeft覆盖,开始移动文本控件hDown
pos = get(hDown, 'position');
pos(1) = pos(1)-delt;
set(hDowm, 'Position', pos);
end
end
drawnow; %重绘窗口
pause(0.1); %暂停0.1秒后继续执行循环
end
1.6曲面对象
建立曲面对象可以使用surface函数,其调用格式为
句柄变量= surface(x,y,z,属性名1,属性值1,属性名2,属性值2,…)
其中对x.y.z的解释与高层曲面函数mesh和surf等一样,其他参数的解释与前面介绍过的figure和axes等函数类似。
曲面对象的属性如下图所示。
- EdgeColor属性:取值是代表某颜色的字符或RGB值,还可以是flat. interp或
none,默认值为黑色。其定义了曲面网格线的颜色或着色方式。
如下
clc;clear;close all;
[x,y,z]= peaks(20);
axes( 'xlim',[-3 3], 'ylim',[-3 3]);
view([- 37.5 30]);
h= surface(x, y,z);
pause
set(h, 'edgecolor', 'r')
pause
set(h, 'edgecolor',[0 0 1])
pause
set(h, 'edgecolor', 'flat')
pause
set(h, 'edgecolor', 'interp')
pause
set(h, 'edgecolor', 'none')
pause
set(h, 'edgecolor','k')
-
FaceColor属性:取值与EdgeColor属性相似,默认值为flat。定义了曲面网格片的颜色或着色方式。
-
LineStyle属性:定义曲面网格线的类型。
-
Marker属性:定义曲面数据点标记符号,默认值为none.
-
MarkerSize属性:定义曲面数据点标记符号的大小,默认值为6磅。
-
XData、YData ,ZData属性:取值为数值向量或矩阵,分别代表曲线对象的3个坐标轴数据
例 利用曲面对象绘制三维曲面z= cos(x+1)。
x= 0:pi/20:2*pi;
[X,Y] = meshgrid(x);
Z=cos(X+ 1);
axes('view',[-37.5 , 80])
hs = surface(X,Y,Z, 'facecolor', 'g', 'edgecolor', 'r');
grid on
set(get(gca, 'xlabel'), 'string', 'x- axis');
set(get(gca, 'ylabel'), 'string', 'y- axis');
set(get(gca, 'zlabel'), 'string', 'z- axis');
title( 'mesh- surf')
set( hs, 'facecolor', 'interp')