MATLAB的GUI

 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');
GUI 1

findobj:特殊屬性的圖形對象 (doc findobj)正則表達式

語法:ide

1.findobj:函數

findobj返回根對象的句柄和全部子對象(findobj returns handles of the root object and all its descendants without assigning the result to a variable.)字體

2.h = findobj:ui

返回根對象的句柄和全部子對象spa

3.h = findobj('PropertyName',PropertyValue,...)3d

返回全部屬性名爲‘PropertyName’,屬性值爲'PropertyValue'的圖形對象的句柄。能夠指定多個屬性/值對。code

4.h = findobj('PropertyName',PropertyValue,'-logicaloperator', PropertyName',PropertyValue,...)regexp

 -logicaloperator能夠取值:orm

-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])

GUI 
 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
2
 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');
GUI 3
 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));
GUI 4
 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'');'
GUI 5
 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'');'
GUI6
 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');
GUI 7
 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');
GUI 8
 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)
GUI9
相關文章
相關標籤/搜索