原书见
https://www.ilovematlab.cn/thread-568839-1-1.html
https://zhidao.baidu.com/question/571294365.html
进度条
clear; %清空基本工作空间的变量
isCanceled = false; %表征是否按下了【取消】按钮
hWaitbar = waitbar(0, '请等待...', 'Name', '进度条', 'CreateCancelBtn',...
'isCanceled=true;'); %创建进度为0的进度条
btnCancel = findall(hWaitbar, 'style', 'pushbutton'); %查找【取消】按钮
set(btnCancel, 'string', '取消', 'fontsize', 10); %设置【取消】按钮的String为“取消”
for i = 1 : 100 %循环更新进度显示
waitbar(i/100, hWaitbar, ['进度完成' num2str(i) '%']);
pause(0.1); %每0.1秒完成1%的进度
if isCanceled %若按下了【取消】按钮,跳出循环
break;
end
end
%% 退出循环后,关闭进度条并清除进度条句柄变量
if ishandle(hWaitbar) %或者if exist('hWaitbar', 'var')
delete(hWaitbar);
clear hWaitbar;
end
结果展示
绘图进度
set(gcf, 'visible', 'on'); %设置当前坐标轴用于绘制曲线,注意:gcf、gca创建的figure默认是隐藏的
%% 创建置于屏幕前端的进度条,动态显示绘图进度
h = waitbar(0, '开始绘图...', 'WindowStyle', 'modal');
t = 0 : 0.01 : pi;%数据的横坐标
for i = 1 : 10
plot(t, sin(2*pi*i*t));%绘制数据曲线
waitbar(i/10, h, ['已完成' num2str(10*i) '%']);%更新进度条的进度和标题
pause(1); %延迟1秒
end
close(h); %关闭进度条
结果展示
图片添加文字
%% 创建一个隐藏的窗口,并调整窗口位置到屏幕中间
hFigure = figure('menubar', 'none', 'NumberTitle', 'off', 'position',...
[1000 1000 720 450], 'name', '江楼感旧', 'Visible', 'off');
movegui(hFigure, 'center');
%% 创建坐标轴,用于显示背景图片和文本
hAxes = axes('visible', 'off', 'units', 'normalized', 'position', [0 0 1 1]);
axis off;
%% 显示图片
cData = imread('pic.jpg');
image(cData);
%% 要显示的文本内容
strCell = {'独上江楼思渺然,', '月光如水水如天。',...
'同来望月人何在?', '风景依稀似去年。'};
%% 逐列显示文本
for i = 1 : numel(strCell) %穷举每条诗句
strTemp = strCell{i}; %获取第i条诗句
str = [strTemp; 10*ones(1, length(strTemp))]; %诗句的每个字后添加一个换行符
str = str(:)'; %获取添加了换行符的诗句字符串
text('string', str, 'position', [250-50*i 150], 'Horizontal', 'right',...
'FontName', '华文楷体', 'FontSize', 18, 'FontWeight', 'bold', 'color', 'red');
end
%% 显示窗口
set(hFigure, 'Visible', 'on');
处理后
使用patch
x = 2 : 0.01 : 4; %数据x
y1 = x .^ 2; %数据y1
y2 = x .^ 3; %数据y2
line(x, y1); %由数据x和y1绘制曲线
line(x, y2); %由数据x和y2绘制曲线
%以上述两条曲线上的点为xData和yData,绘制patch对象
patch('xdata', [x fliplr(x)], 'ydata', [y1 fliplr(y2)], 'FaceColor', 'r')
matlab中patch函数详解
官方文档对patch的描述
句法
patch(X,Y,C)
patch(X,Y,Z,C)
patch(‘XData’,X,‘YData’,Y)
patch(‘XData’,X,‘YData’,Y,‘ZData’,Z)
patch(‘Faces’,F,‘Vertices’,V)
patch(S)
patch(___,Name,Value)
patch(ax,___)
p = patch(___)
描述
参数表 | 功能 |
---|---|
patch(X,Y,Z) | 使用元素X和Y每个顶点的坐标创建一个或多个填充多边形。 patch按指定顺序连接顶点。要创建一个多边形,请指定X和Y作为向量。要创建多个多边形,请指定每个列对应多边形的矩阵X和Y矩阵。C确定多边形颜色。 |
patch(X,Y,Z,C) | 创建在3-d的多边形坐标使用X,Y和Z。要在三维视图中查看多边形,请使用该view(3)命令。C确定多边形颜色。 |
p = patch(___) | 返回包含所有多边形数据的补丁对象。 |
颜色数据说明
编辑公式
syms t x f g b
f = taylor(log(1+x));
g = [cos(t) sin(t); -sin(t) cos(t)];
b = eig(sym(pascal(2)));
%% 在窗口内显示表达式
figure('name','符号运算结果的窗口显示BY罗华飞');
hAxes = axes('units','normalized','position',[0,0,1,1],'visible','off'); %创建隐藏坐标轴
text(0.1,0.25,['f = $', latex(f), '$'],'interpreter','latex','fontsize',16); %显示表达式f
text(0.1,0.55,['g = $', latex(g), '$'],'interpreter','latex','fontsize',16); %显示表达式g
text(0.1,0.85,['b = $', latex(b), '$'],'interpreter','latex','fontsize',16); %显示表达式b
简易时钟
%% 创建合适大小、隐藏的GUI窗口
hFigure = figure('menubar', 'none', 'NumberTitle', 'off', 'position',...
[200 150 300 150], 'name', '简易时钟', 'Visible', 'off');
movegui(hFigure, 'center'); %窗口位置调整好屏幕中间
%% 设置UI控件缺省的背景色、位置单位和字体大小
set(0, 'DefaultuicontrolBackgroundColor', get(hFigure,'color'))
set(0, 'DefaultuicontrolUnits', 'points')
set(0, 'DefaultuicontrolFontsize', 12)
%% 获取当前时钟,用于初始化文本控件显示的时间
nowTime = floor(clock);
%% 创建所需要的UI控件
yearDisp = uicontrol('style', 'edit', 'enable', 'inactive', 'BackgroundColor',...
'w', 'horizontal', 'right', 'position', [20 80 30 20], 'string',...
num2str(nowTime(1)));
uicontrol('style', 'text', 'string', '年', 'fontsize', 14, 'position', [55 80 20 20]);
monDisp = uicontrol('style', 'edit', 'enable', 'inactive','BackgroundColor', 'w',...
'horizontal', 'right', 'position', [80 80 30 20], 'string', num2str(nowTime(2)));
uicontrol('style', 'text', 'string', '月', 'fontsize', 14, 'position', [115 80 20 20]);
dayDisp = uicontrol('style', 'edit', 'enable', 'inactive', 'BackgroundColor', 'w',...
'horizontal', 'right', 'position', [140 80 30 20], 'string', num2str(nowTime(3)));
uicontrol('style', 'text', 'units', 'points', 'string', '日', 'fontsize',...
14, 'position', [175 80 20 20]);
timeDisp = uicontrol('style', 'edit', 'enable', 'inactive', 'BackgroundColor',...
'w', 'horizontal', 'right', 'position',[40 30 80 20], 'string',...
[num2str(nowTime(4)) ':' num2str(nowTime(5)) ':' num2str(nowTime(6))]);
uicontrol('string', '关闭', 'position', [150 30 50 20], 'callback', 'isClosed = true;close');
%% 显示窗口
set(hFigure, 'Visible', 'on');
%% 设置全局标志
isClosed = false; %表征窗口是否执行关闭操作
%% 循环更新时间显示
while ishandle(hFigure)
nowTime = floor(clock); %更新当前时刻
set(yearDisp, 'string', num2str(nowTime(1))); %设置年
set(monDisp, 'string', num2str(nowTime(2))); %设置月
set(dayDisp, 'string', num2str(nowTime(3))); %设置日
set(timeDisp, 'string', [num2str(nowTime(4), '%2d'), ':', num2str(nowTime(5), '%2d'),...
':', num2str(nowTime(6), '%2d')]); %设置时分秒
pause(1); %暂停1秒
if isClosed %检查isColosed标志,所点击了【关闭】按钮,跳出循环
break;
end
end
交互曲线设置
h0 = figure('menubar','none',... %创建窗口
'position',[200 60 450 450],...
'numbertitle', 'off',...
'name','例4.4.1:设置曲线的形状与颜色');
h1 = axes('parent',h0,... %创建坐标轴
'position',[0.15 0.45 0.75 0.45],...
'visible','on');
xlabel('自变量x'); %x轴标签
ylabel('函数值y'); %y轴标签
title('y=sin(x)'); %标题
x = 0 : 0.1 : 2 * pi; %x轴数据
k = line(x,sin(x)); %绘制数据曲线
set(0,'DefaultUicontrolfontsize',12) %设置控件默认的字体大小
p1 = uicontrol('parent',h0,... %创建【加号】按钮
'string','加号',...
'position',[80 120 50 30],...
'callback','set(k,''marker'',''+'')');
p2 = uicontrol('parent',h0,... %创建【圆圈】按钮
'string','圆圈',...
'position',[200 120 50 30],...
'callback','set(k,''marker'',''o'')');
p3 = uicontrol('parent',h0,... %创建【星号】按钮
'string','星号',...
'position',[320 120 50 30],...
'callback','set(k,''marker'',''*'')');
c1 = uicontrol('parent',h0,... %创建【红色】按钮
'string','红色',...
'position',[80 80 50 30],...
'callback','set(k,''color'',''r'')');
c2 = uicontrol('parent',h0,... %创建【绿色】按钮
'string','绿色',...
'position',[200 80 50 30],...
'callback','set(k,''color'',''g'')');
c3 = uicontrol('parent',h0,... %创建【蓝色】按钮
'string','蓝色',...
'position',[320 80 50 30],...
'callback','set(k,''color'',''b'')');
s1 = uicontrol('parent',h0,... %创建【实线】按钮
'string','实线',...
'position',[80 40 50 30],...
'callback','set(k,''LineStyle'',''-'')');
s2 = uicontrol('parent',h0,... %创建【虚线】按钮
'string','虚线',...
'position',[200 40 50 30],...
'callback','set(k,''LineStyle'',''--'')');
s3 = uicontrol('parent',h0,... %创建【无线】按钮
'string','无线',...
'position',[320 40 50 30],...
'callback','set(k,''LineStyle'',''none'')');
小结
总的来说,MATLAB提供了丰富的GUI接口,程序设计相对容易,还有很多工具箱函数没有尝试,这里也不能逐一列举,还需要在平时的学习和工作中融会贯通。MATLAB GUI设计并不是多新奇的玩意,任何编程语言都可以做可视化处理,只不过平时用MATLAB更多的是用于科学计算和绘制plot图像,很少会用到GUI。本次是学习MATLAB GUI的一些初步尝试,希望这篇blog能够给后来的学习开一个好头。