MATLAB的GUI
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % 常使用的对象查看和设置函数 2 % 1.get、set函数 3 get(0) % 获得句柄值为0的对象的属性,即显示器对象属性 4 5 plot([0:10]); % 绘制一幅图 6 title('示例'); % 增加text对象 7 8 % 获得figure的所有子对象 9 allchild(gcf) 10 11 % 查看子对象类型 12 get(ans(1)) % type属性可以看到子对象类型 13 14 % 获得静态文本“示例”的句柄并进行设置 15 h = findobj(allchild(gca), 'String', '示例'); 16 set(h, 'FontSize', 20, 'FontWeight', 'bold');
findobj:特殊属性的图形对象 (doc findobj)
语法:
1.findobj:
findobj返回根对象的句柄和所有子对象(findobj returns handles of the root object and all its descendants without assigning the result to a variable.)
2.h = findobj:
返回根对象的句柄和所有子对象
3.h = findobj('PropertyName',PropertyValue,...)
返回所有属性名为‘PropertyName’,属性值为'PropertyValue'的图形对象的句柄。可以指定多个属性/值对。
4.h = findobj('PropertyName',PropertyValue,'-logicaloperator', PropertyName',PropertyValue,...)
-logicaloperator可以取值:
-and
-or
-xor
-not
等
5.h = findobj('-regexp','PropertyName','regexp',...)
属性名可以使用正则表达式
6.h = findobj('-property','PropertyName')
如果存在‘PropertyName’这个属性名,就返回此图形句柄
7.h = findobj(objhandles,...)
限制搜索范围为objhandles和他们的子图中
8.h = findobj(objhandles,'-depth',d,...)
指定搜索深度,深度参数'd'控制遍历层数,d为inf表示遍历所有层,d为0等同d='flat'
9.h = findobj(objhandles,'flat','PropertyName',PropertyValue,...)
'flat'限制搜索范围只能是当前层,不能搜索子图。
如果句柄指向一个不存在的图形,findobj返回一个错误。
findobj正确匹配任何合法属性值,例如:
findobj('Color','r')
找到所有color值为红的对象。
为了寻找满足指定条件的额handle对象,我们可以使用handle.findobj。
例子:
在当前坐标下查找所有直线对象:
h = findobj(gca,'Type','line') %gca为当前坐标的句柄
查找Label属性设为'foo'和String设为'bar'的所有对象:
h = findobj('Label','foo','-and','String','bar');
查找String不为'foo'也不为'bar'的所有对象:
h = findobj('-not','String','foo','-not','String','bar');
h = findobj('String','foo','-and','Tag','button one',...
'-and','-not',{'Color','red','-or','Color','blue'})
Find all objects for which you have assigned a value to the Tag property (that is, the value is not the empty string ''):
h = findobj('-regexp','Tag','[^'']')
Find all children of the current figure that have their BackgroundColor property set to a certain shade of gray ([.7 .7 .7]). This statement also searches the current figure for the matching property value pair.
h = findobj(gcf,'-depth',1,'BackgroundColor',[.7 .7 .7])
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % 对象操作示例 2 % h= figure ; get(h); 3 %获取能够使用的句柄 4 hf = figure('Units', 'Normalized', ... 5 'Position', [0.2 0.3 0.5 0.5], ... 6 'Menu', 'none'); 7 8 ha = axes('Parent', hf, 'Units', 'Normalized', ... 9 'Position', [0.1 0.1 0.8 0.8]); 10 11 hl = line('Parent', ha, 'XData', [0:0.01:7], ... 12 'YData', sin([0:0.01:7]), 'Color', 'r', ... 13 'LineWidth', 3); 14 15 cstring = 'gbkmy'; 16 17 for k = 1:5 18 pause(3); 19 set(hl, 'Color', cstring(k)); 20 end
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % 底层代码实现GUI 2 hf = figure(... 3 'Units', 'Normalized', ... 4 'Position', [0.2 0.2 0.6 0.5], ... 5 'Menu', 'none', ... 6 'Color', 'w'); 7 8 ha = axes('Parent', hf, ... 9 'Units', 'Normalized', ... 10 'Position', [0.1 0.1 0.6 0.8], ... 11 'Box', 'off', ... 12 'NextPlot', 'add'); 13 14 hb1 = uicontrol('Parent', hf, ... 15 'Units', 'Normalized', ... 16 'Position', [0.75 0.2 0.15 0.1], ... 17 'Style', 'pushbutton', ... 18 'String', 'sin', ... 19 'Callback', 'plot(sin([0:0.01:6]))'); 20 21 hb2 = uicontrol('Parent', hf, ... 22 'Units', 'Normalized', ... 23 'Position', [0.75 0.4 0.15 0.1], ... 24 'Style', 'pushbutton', ... 25 'String', 'cos', ... 26 'Callback', 'plot(cos([0:0.01:6]))'); 27 28 hb3 = uicontrol('Parent', hf, ... 29 'Units', 'Normalized', ... 30 'Position', [0.75 0.6 0.15 0.1], ... 31 'Style', 'pushbutton', ... 32 'String', 'clear', ... 33 'Callback', 'try,delete(allchild(ha));end');
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % 常用对象的属性 2 % % 1.figure 3 % hf = figure; 4 % get(hf); 5 % 6 % % 改变颜色 7 % set(hf, 'Color', 'w'); 8 % set(hf, 'Menubar', 'none'); 9 % set(hf, 'NumberTitle', 'off', 'Name', '演示'); 10 % set(hf, 'ReSize', 'off'); 11 % pause(3) 12 % set(hf, 'Visible', 'off'); 13 % pause(3) 14 % set(hf, 'Visible', 'on'); 15 % 16 % set(hf, 'WindowStyle', 'modal'); 17 % 18 % set(hf, 'WindowKeyPressFcn', 'closereq'); 19 % 20 % set(hf, 'WindowButtonDownFcn', 'closereq'); 21 % 22 % hb = uicontrol('Style', 'pushbutton', 'Callback', 'closereq'); 23 24 % 2.axes 25 ha = axes; 26 get(ha) 27 set(ha, 'NextPlot', 'add'); 28 plot([0:100]); 29 30 plot(sin(0:0.01:3));
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % text 2 hf = axes; 3 ht = text(1, 1, '示例'); 4 5 get(ht) 6 7 text('String', '\int_0^x dF(x)', 'Position', [0.5 .5]); 8 9 text('interpreter', 'latex', 'String', '$$ \int_0^x dF(x) $$', 'Position', [0.2 .2]); 10 11 % 原始的语句写出来 12 plot(x); 13 % 在原始语句两遍加上单引号 14 'plot(x);' 15 % 当原始语句中含有引号,那么将原始的单引号都改为两个单引号,然后再最外层加上一对单引号 16 'plot(x, y, ''r'');'
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % text 2 hf = axes; 3 ht = text(0.1, 1, '示例'); 4 5 get(ht) 6 7 text('String', '\int_0^x dF(x)', 'Position', [0.5 .5]); 8 9 text('interpreter', 'latex', 'String', '$$ \int_0^x dF(x) $$', 'Position', [0.2 .2]); 10 11 % 原始的语句写出来 12 plot(x); 13 % 在原始语句两遍加上单引号 14 'plot(x);' 15 % 当原始语句中含有引号,那么将原始的单引号都改为两个单引号,然后再最外层加上一对单引号 16 'plot(x, y, ''r'');'
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % uigetfile 2 uigetfile 3 4 doc uigetfile 5 6 % 规定打开文件类型 7 uigetfile('*.m'); 8 9 % 输出参数意义 10 [a, b, c] = uigetfile('*.m'); 11 12 [a, b, c] = uigetfile('*.txt'); 13 if c == 1 14 load(fullfile(b, a)); 15 end 16 17 uigetfile('*.m', '实例', 'default.m'); 18 19 % uiputfile 20 uiputfile 21 22 doc uiputfile 23 [a, b, c] = uiputfile('*.m');
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % 颜色设置对话框 2 uisetcolor 3 4 doc uisetcolor 5 6 c = uisetcolor; 7 8 c = uisetcolor([1 0 0]); 9 10 h = plot([0:10]); 11 c = uisetcolor(h); 12 13 figure; 14 b = uicontrol('Parent', gcf, 'String', '颜色设置', 'Style', 'pushbutton', 'Callback', ... 15 'c = uisetcolor; set(b, ''BackgroundColor'', c);'); 16 17 % 字体设置对话框 18 uisetfont 19 20 doc uisetfont 21 22 S = uisetfont(b); 23 24 figure; 25 b = uicontrol('Parent', gcf, 'String', '颜色设置', 'Style', 'pushbutton', 'Callback', ... 26 'uisetfont(b);', 'Position', [0.2 .2 0.8 0.8], 'Units', 'Normalized');
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 % 进度条 2 % waitbar 3 h = waitbar(0, '实例'); 4 get(h) 5 6 % 获得进度条的子对象 7 get(get(h, 'Children')) 8 9 ha = get(h, 'Children'); 10 11 % 获得坐标轴子对象的子对象内容 12 get(ha, 'Children') 13 14 get(ans(1)) 15 get(ans(2)) 16 17 hrand = waitbar(0.3, '颜色') 18 19 ha1 = get(hrand, 'Children'); 20 hac = get(ha1, 'Children'); 21 hapa = findall(hac, 'Type', 'patch'); 22 set(hapa, 'Facecolor', 'k') 23 24 doc waitbar 25 26 waitbar(0.5, hrand)