Microsoft Word - GUI design.doc

Similar documents

à

ì ì



è





ttian


ú ì

ì



ò ó ì á è ó












í

ǎ ì


ü Ä ä ä ï ï ü ä ä


` ù




í

é




ü Ä ä ä ï ï ü ä ä


à è




ttian








2 3 5


` ` ` ` ō ù ù ǐ ù ` ǐ ` ` ` ù è ó ù `

ú ú


1







ó


ō


í ì






ì





` ` ` ` ǐ ù ó ` ù ` `















` ` `


Transcription:

GUI 程式設計 壹 圖形元件 一 認識圖形元件 hndlgraf 指令 Handle graph 的縮寫 說明 二 認識圖形元件的 handle 指令 get(h, 'pr') get(h) set(h, 'pr1', 'val1', 'pr2', 'val2') set(h) 說明取得 handle h 的某屬性 (properity) 之數值取得 handle h 的所有屬性之數值設定 handle h 的某屬性 (properity) 之數值設定 handle h 的所有屬性之數值 x=linspace(-3,3,100); y=normpdf(x,0,1); h=plot(x,y) get(h) get(h, 'marker') set(h, 'marker', 'o') set(h, 'linewidth', 2, 'markersize', 16) 指令 Marker Markersize markeredgecolor markerfacecolor linewidth 標誌符號標誌符號大小標誌符號的邊框標誌符號的內面線段寬度 說明 三 取得繪圖區與繪圖視窗的 handle 指令 說明 ha=gca 取得繪圖區之 handle, 若無則回應 [ ] hf=gcf 取得繪圖視窗之 handle, 若無則回應 [ ] 1

四 關閉繪圖視窗 close close all close (h) 指令 說明關閉目前的視窗關閉所有的繪圖視窗關閉 handle 為 h 的繪圖視窗 2

貳 認識 UI 元件 (user interface) 一 UI 元件基本認識 指令 h=figure('pr', 'va1', 'pr2', 'va2',...) h=uicontrol('pr', 'va1', 'pr2', 'va2',...) 說明建立一個繪圖視窗並設定 handle 與其屬性值建立一個 UI 控制元件並設定 handle 與其屬性值 h1=figure('position', [90 50 200 120], 'Menubar','none','Name','MY GUI') get(h1) set(h1, 'color', 'white') h2=uicontrol('style', 'pushbutton', 'Position', [50 40 100 30],'String', 'push me') 3

set(h2, 'Position', [50 20 100 30]) 二 UI 控制文件設定 Style 說明 用途 'pushbutton' 按鈕 'radio' 選擇按鈕 單選 'toggle' 雙態按鈕 按下與彈開兩種狀態 'checkbox' 核取方塊 複選 'edit' 文字方塊 可輸入文字 'popup' 下拉選單 單選 'listbox' 選擇表單 單選或複選 'slider' 捲軸 拖拉方式輸入數值 'text' 靜態文字方塊 靜態文字 三 UI 控制文件的屬性設定 Style 說明 用途 BackgroundColor [red green blue] 背景顏色 CallBack 設定該元件被觸動時所要執行的指令 Enable [on {off} ] 設定元件是否在可用的情況 ForegroundColor [red green blue] 前景顏色 HorizontalAlignment [left {center} right] 文字的對齊方式 Max 設定捲軸最大值 Min Position [left button width height] 元件位置與大小 String Style {pushbutton} radiobutton... SliderStep 設定捲軸每次可移動的大小 TooltipString 設定工具提示的小字串 Value 4

Tag 設定元件標籤, 以方便函數找尋 Visible [{on} off ] 設定元件是否顯示出來 5

參 建立按鈕與核取方塊 close all figure('position', [90 50 200 120], 'Menubar','none','Name','MY GUI', 'Color', 'white') uicontrol('style', 'checkbox', 'Position', [20 50 80 20],'String', 'checkbox') uicontrol('style', 'radio', 'Position', [20 20 80 20],'String', 'Radio button', 'Value', 1) uicontrol('style', 'toggle', 'Position', [110 50 80 20],'String', 'Toggle button') 6

肆 建立動態與靜態文字方塊 close all figure('position', [90 50 200 120], 'Menubar', 'none', 'Name', 'MY GUI') uicontrol('style', 'edit', 'Position', [20 20 80 80], 'String', 'This is an editable text', 'HorizontalAlignment', 'left', 'Max', 2, 'Min', 0) uicontrol('style', 'text', 'Position', [115 70 70 30], 'String', 'This is a static text') 7

伍 建立選擇表單與下拉選單 close all figure('position', [50 50 200 120], 'Menubar', 'none', 'Name', 'MY GUI') h1=uicontrol('style', 'popup', 'Position', [20 80 70 20], 'String', 'apple orange banana') h2=uicontrol('style', 'listbox', 'Position', [110 40 70 60], 'String', 'cat dog pig', 'Max', 2, 'Min', 0) 8

陸 建立捲軸 figure('position', [50 50 200 120], 'Menubar', 'none', 'Name', 'MY GUI') h1=uicontrol('style', 'slider', 'Position', [20 80 70 20]) h2=uicontrol('style', 'slider', 'Position', [130 20 20 80]) 9

柒 對話方塊的設計 指令 msgbox(message, title, icon) questdlg(question, title, b1, b2,...) inputdlg(prompt, title) 說明建立對話方塊建立可供選擇的對話方塊建立輸入對話方塊 msgbox(' 輸入必須為整數 ', 'Warning msg', 'warn') msgbox(' 輸入錯誤, 請重新輸入 ', 'Error msg', 'error') msgbox(' 請求協助 ', 'Help msg', 'help') questdlg('are you sure?', 'Confirm', 'yes', 'no', 'no') 10

questdlg('are you sure?', 'Confirm' ) str=inputdlg('input a number', 'Input dialog') num=str2num(str{1}) str=inputdlg({'number1', 'number2'},'input dialog') 11

捌 簡單的事件處理 一 按鈕的事件設計 figure('position',[80 80 270 150], 'Menubar', 'none'); hclose=uicontrol('style', 'pushbutton', 'String', 'Close'); hwhite=uicontrol('style', 'pushbutton', 'String', 'White', 'Position',[20 80 60 20]); hred=uicontrol('style', 'pushbutton', 'String', 'Red', 'Position',[20 110 60 20]); htxt=uicontrol('style', 'text', 'Position',[100 20 150 110]); cmd1='set(htxt, ''BackgroundColor'', ''white'')'; cmd2='set(htxt, ''BackgroundColor'', ''red'')'; set(hclose, 'Callback', 'close'); set(hwhite, 'Callback', cmd1); set(hred, 'Callback', cmd2); 二 將 Callback 所要執行的指令寫成 M 檔案 %script17_2.m figure('position',[80 80 270 150], 'Menubar', 'none'); hclose=uicontrol('style', 'pushbutton', 'String', 'Close'); hwhite=uicontrol('style', 'pushbutton', 'String', 'White', 'Position',[20 80 60 20]); hred=uicontrol('style', 'pushbutton', 'String', 'Red', 'Position',[20 110 60 20]); htxt=uicontrol('style', 'text', 'Position',[100 20 150 110]); cmd1='set(htxt, ''BackgroundColor'', ''white'')'; cmd2='set(htxt, ''BackgroundColor'', ''red'')'; set(hclose, 'Callback', 'close_check'); set(hwhite, 'Callback', cmd1); set(hred, 'Callback', cmd2); 12

result=questdlg(' 確定要關閉?', 'Window closing', 'yes', 'no', 'no'); if strcmp(result, 'yes') close end ( 一 ) 下拉選單與核取方塊的練習 : 製作 Z 分配與 t 分配圖形比較 GUI % Z À t»pt À t ñ û figure('position',[80 80 280 220],'Menubar','none'); axes('position',[0.1 0.25 0.8 0.65]); % «Ø ßà ¹Ï Ï ó chk=uicontrol('style','checkbox','string','grid',... 'position',[20 10 50 20]); pop=uicontrol('style','popupmenu','string','1 5 10 30 100 1000',... 'position',[80 10 50 20]); set(pop,'callback','ztplot'); % í U Ô ï³æ³q ï¾ü É A«K õ æztplot.m set(chk,'callback','grid'); % í Ö ú è ô³q ï¾ü É A«K õ ægrid«ü O 13

switch get(pop, 'Value') case 1 df=1; case 2 df=5; case 3 df=10; case 4 df=30; case 5 df=100; case 6 df=1000; end x=linspace(-3,3,100); y=normpdf(x,0,1); y1=tpdf(x, df); plot(x,y,'-r', x, y1, '--b'); legend('z distribution', 't distribution',2); if get(chk, 'Value')==1 end grid on 14

( 二 ) 選擇按鈕的練習 figure('position',[80 80 280 220], 'Menubar', 'none'); axes('position', [0.1 0.25 0.8 0.65]); h(1)=uicontrol('style', 'radio', 'String', 'red', 'Value',1,'Position',[20 10 50 20]); h(2)=uicontrol('style', 'radio', 'String', 'green','position',[70 10 50 20]); h(3)=uicontrol('style', 'radio', 'String', 'blue','position',[120 10 50 20]); set( h(1), 'Callback', 'plot_data(h, ''r'', 1)' ); set( h(2), 'Callback', 'plot_data(h, ''g'', 2)' ); set( h(3), 'Callback', 'plot_data(h, ''b'', 3)' ); plot_data(h, 'r', 1); function plot_data(h, color, i) set(h, 'Value', 0); set(h(i),'value',1); x=linspace(-3,3,100); y=normpdf(x,0,1); lin_color=color; plot(x,y,lin_color, x, y, '.r'); 15

( 三 ) 選擇表單的練習 figure('position',[80 80 280 220], 'Menubar', 'none'); hlist1=uicontrol('style', 'listbox', 'Position',[20 40 120 120], 'Max',2, 'Min', 0, 'HorizontalAlignment', 'left', 'String', 'var1 var2 var3' ); hlist2=uicontrol('style', 'listbox', 'Position',[155 40 120 120], 'HorizontalAlignment', 'left'); hcopy=uicontrol('position', [90 10 60 20], 'String', 'Select'); hclear=uicontrol('position', [160 10 60 20], 'String', 'Clear'); clr='set(hlist2, ''String'','' '' )'; set(hcopy, 'Callback', 'item_copy'); set(hclear, 'Callback', clr); val=get(hlist1, 'Value'); str=get(hlist1, 'String'); set(hlist2, 'String', str(val,:)); 16

( 四 ) 捲軸的練習 figure('position',[80 80 280 220], 'Menubar', 'none'); axes('position', [0.1 0.25 0.8 0.65]); hsld=uicontrol('style', 'slider', 'Position', [30 20 100 20],'Max', 1000, 'Min', 1, 'Value', 1, 'SliderStep', [1/1000, 10/1000]); df=get(hsld, 'Value') ; chi2=chi2inv(.95, df) ; hedit1=uicontrol('style', 'edit', 'Position', [150 20 50 20], 'String', df); hedit2=uicontrol('style', 'edit', 'Position', [200 20 50 20], 'String', chi2); set(hsld, 'Callback', 'chisqplot'); df=get(hsld, 'Value'); chi2=chi2inv(.95, df) ; set(hedit1, 'String', round(df) ); set(hedit2, 'String', chi2 ); x=linspace(0,100,1000); 17

y=chi2pdf(x,df); plot(x,y) 18