Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB 程式設計入門篇 方煒 台大生機系

Similar presentations


Presentation on theme: "MATLAB 程式設計入門篇 方煒 台大生機系"— Presentation transcript:

1 MATLAB 程式設計入門篇 方煒 台大生機系
握把式圖形與 GUI 設計 方煒 台大生機系

2 GUI:Graphical User Interface
MATLAB 程式設計入門篇 Handle Graphics概念 握把式圖形Handle Graphics概念 每一個圖形元件視為一個物件Object Object都有一個獨一無二的握把Handle GUI:Graphical User Interface

3

4 MATLAB 程式設計入門篇 圖形物件的階層結構 圖形物件階層結構(Hierarchy) Rectangle

5 MATLAB 程式設計入門篇 說明範例

6 圖形物件的性質存取 以 GUI 進行圖形物件的性質存取 MATLAB 5.x MATLAB 6.x MATLAB 7.x
以命令列進行圖形物件的性質存取

7 MATLAB 5.x MATLAB 5.x >>propedit(gcf)
gcf 傳回使用中圖形握把,開啟「性質編輯器」(Property Editor),產生的新視窗如下:

8 MATLAB 程式設計入門篇 MATLAB 5.x 性質編輯器 MATLAB 5.x 物件瀏覽 性質陳列 性質修改

9 MATLAB 6.x Propedit01.m MATLAB 6.x >>peaks; % 畫出 peaks 3D 圖

10 MATLAB 程式設計入門篇 MATLAB 6.x 性質編輯器 MATLAB 6.x

11 MATLAB 程式設計入門篇

12 MATLAB 7.x 性質編輯器 MATLAB 7.x >>peaks; % 畫出 peaks 3D 圖
>>propedit; % 開啟性質編輯器

13 MATLAB 7.x 性質編輯器開啟方法 變更物件性質 (自己玩吧!) 命令視窗Propedit
工作列上”View” ”Property Editor” 變更物件性質 按工作列上白色箭頭””點選所需物件 Property Editor 中 ”Inspector” 按鈕 (自己玩吧!)

14 Property Editor & Property Inspector
MATLAB 程式設計入門篇 Property Editor & Property Inspector 兩者效果相同,但Property Inspector較有助於m檔案中程式撰寫的學習與修改。

15 圖形修改介面:繪圖工具(Plot Tools)
MATLAB 程式設計入門篇 圖形修改介面:繪圖工具(Plot Tools)

16 圖形物件的性質存取 以命令列進行圖形物件的性質存取 set 設定某個性質的值 get 取得某個性質的值
MATLAB 程式設計入門篇 圖形物件的性質存取 以命令列進行圖形物件的性質存取 set 設定某個性質的值 get 取得某個性質的值 findobj 在握把式圖形的階層式結構中,找出您要的物件。 xpsound、travel、truss、lorenz 等指令

17 set範例:hg01.m, hg02.m t = 0:0.1:4*pi; y = exp(-t/5).*sin(t);
MATLAB 程式設計入門篇 set範例:hg01.m, hg02.m t = 0:0.1:4*pi; y = exp(-t/5).*sin(t); h = plot(t, y); % h 為曲線的握把 set(h,'Linewidth',3); % 將曲線寬度改為 3 set(h,'Marker','o');%將曲線的線標改成小圓圈 set(h,'MarkerSize',20);% 將線標的大小改成 20

18 get範例: >>get(h,'LineWidth') %取得曲線寬度 ans =3;
MATLAB 程式設計入門篇 get範例: >>get(h,'LineWidth') %取得曲線寬度 ans =3; >> get(h,'Color') %取得曲線顏色 ans =

19 MATLAB 程式設計入門篇 單獨使用set(h) 列出h之所有性質

20 findobj範例:findobj01.m % k=findobj('tag','figure number1'); % close(k)
MATLAB 程式設計入門篇 findobj範例:findobj01.m % k=findobj('tag','figure number1'); % close(k) figure('tag','figure number1','name','Fig 01'); plot(rand(10,2)); % 畫出兩條曲線 h=findobj(0,'type','line') % 找出曲線的握把 set(h,'LineWidth',3);

21 MATLAB 程式設計入門篇 MATLAB GUI GUI 的設計 M 檔案 GUIDE 發展環境

22 MATLAB 程式設計入門篇 M檔案 M 檔案的GUI 設計 Uicontrol Mouse Events

23 uicontrol範例1 uicontrol01.m
MATLAB 程式設計入門篇 uicontrol範例1 uicontrol01.m h = uicontrol; % 產生按鈕 set(h,'String','請按我!'); % 在按鈕表面加入文字「請按我!」 cmd ='fprintf(''有人按我一下喔!\n'');'; % 定義按鈕被按後的反應指令 set(h,'Callback',cmd); % 設定按鈕的反應指令

24 MATLAB 程式設計入門篇 Uicontrol01.m 輸出畫面

25 UI(User Interface)控制物件
MATLAB 程式設計入門篇 UI(User Interface)控制物件 Uicontrol產生UI(User Interface)控制物件 按鈕(Push Button) 滑動棒(Sliding Bar) 圓形按鈕(Radio Button) 框架(Frame) 核計方塊(Check Box) 文字欄位(Edit Box) 列表式選單(List Menu) 下拉式選單(Popup Menu)

26 範例2: uicontrol02.m close all % 關閉所有圖形視窗
MATLAB 程式設計入門篇 範例2: uicontrol02.m close all % 關閉所有圖形視窗 uicontrol('style','push','position',[ ]); uicontrol('style','slide','position',[ ]); uicontrol('style','radio','position',[ ]); uicontrol('style','frame','position',[ ]); uicontrol('style','check','position',[ ]); uicontrol('style','edit','position',[ ]); uicontrol('style','list','position',[ ],'string', '1|2|3|4'); uicontrol('style','popup','position',[ ],'string','one|two|three');

27 MATLAB 程式設計入門篇 uicontrol02.m 畫面輸出

28 MATLAB 程式設計入門篇 另一個較複雜的uicontrol範例 Ui01.m, 需要cb1, cb2, cb3.m

29 Ui01.m % 產生新圖形視窗,其左下角之座標為[30, 30], % 長度為300,高度為200(均以Pixel為單位)
MATLAB 程式設計入門篇 Ui01.m % 產生新圖形視窗,其左下角之座標為[30, 30], % 長度為300,高度為200(均以Pixel為單位) figure('position', [ ]); % 在圖形視窗內產生一個圖軸,其左下角之座標為[0.1, 0.2], % 長度為0.8,高度為0.8(使用標準化的單位,即圖形的左下角為[0, 0], % 長度及高度都是1。) axes('position', [ ]); % 視窗上的第一個圖形,為三度空間的peaks函數。 pointNum = 20; [xx, yy, zz] = peaks(pointNum); surf(xx, yy, zz); axis tight

30 Ui01.m (continue) % 第一個UI控制物件,用以控制背景格線的顯示。 pos1 = [10, 10, 60, 20];
MATLAB 程式設計入門篇 Ui01.m (continue) % 第一個UI控制物件,用以控制背景格線的顯示。 pos1 = [10, 10, 60, 20]; h1 = uicontrol('style', 'checkbox', 'string', 'Grid on', ... 'position', pos1, 'value', 1); % 第二個UI控制物件,用以指定X軸及Y軸的格子點數目。 pos2 = [90, 10, 60, 20]; h2 = uicontrol('style', 'edit', 'string', int2str(pointNum), ... 'position', pos2, 'backgroundColor', [1 1 1]); % 第三個UI控制物件,用以指定顯示曲面所用到的色盤矩陣。 pos3 = [170, 10, 60, 20]; h3 = uicontrol('style', 'popupmenu', ... 'string', 'hsv|gray|hot|cool|pink|jet', ... 'position', pos3);

31 Ui01.m (continue) % 第一個UI控制物件的反應指令為「cb1」 set(h1, 'callback', ‘cb1');
MATLAB 程式設計入門篇 Ui01.m (continue) % 第一個UI控制物件的反應指令為「cb1」 set(h1, 'callback', ‘cb1'); % 第二個UI控制物件的反應指令為「cb2」 set(h2, 'callback', 'cb2'); % 第三個UI控制物件的反應指令為「cb3」 set(h3, 'callback', 'cb3');

32 cb1 grid;

33 cb2 % 取得第二個UI控制物件的數值。 pointNum = round(str2num(get(h2, 'string')));
% 若數字太大或太小,則設定為10。 if pointNum <= 1 | pointNum > 100, pointNum = 10; end set(h2, 'string', int2str(pointNum)); % 根據所得的數字,重畫peaks曲面。 [xx, yy, zz] = peaks(pointNum); surf(xx, yy, zz); axis tight; % 根據第一個UI控制物件,決定是否要畫格線。 if get(h1, 'value')==1, grid on; else grid off;

34 cb3 % 根據第三個UI控制物件來決定使用的色盤矩陣。 switch get(h3, 'value') case 1
colormap(hsv); case 2 colormap(gray); case 3 colormap(hot); case 4 colormap(cool); case 5 colormap(pink); case 6 colormap(jet); otherwise disp('Unknown option'); end

35 Ui02.m Ui01.m 執行須使用四個檔案,軟體管理不便。
MATLAB 程式設計入門篇 Ui02.m Ui01.m 執行須使用四個檔案,軟體管理不便。 所使用的變數,都存於MATLAB工作空間(Workspace),容易造成變數相衝“打槍”及無意中覆蓋。 Ui02.m的寫法(Switchyard Programming—以不同輸入的字串來控制函數的執行)執行結果的比較。 (注意h1與h2、h3間Callback執行的差異) (注意MATLAB工作空間(Workspace)中變數的變化執行後即清空變數)

36 Ui02.m function ui02(action) if nargin == 0, action = 'initialize';
end switch(action) case 'initialize' % 圖形視窗及UI控制物件的初始化。 % 產生新圖形視窗,其左下角之座標為[30, 30], % 長度為300,高度為200(均以Pixel為單位) figH = figure('position', [ ]); % 在圖形視窗內產生一個圖軸,其左下角之座標為[0.1, 0.2], % 長度為0.8,高度為0.8(使用標準化的單位,即圖形的左下角為[0, 0], % 長度及高度都是1。) axes('position', [ ]); % 視窗上的第一個圖形,為三度空間的peaks函數。 pointNum = 20; [xx, yy, zz] = peaks(pointNum); surf(xx, yy, zz); axis tight % 第一個UI控制物件,用以控制背景格線的顯示。 pos1 = [10, 10, 60, 20]; h1 = uicontrol('style', 'checkbox', ... 'tag', 'UI1', ... 'string', 'Grid on', ... 'position', pos1, 'value', 1); Ui02.m

37 % 第二個UI控制物件,用以指定X軸及Y軸的格子點數目。
pos2 = [90, 10, 60, 20]; h2 = uicontrol('style', 'edit', ... 'tag', 'UI2', ... 'string', int2str(pointNum), ... 'position', pos2, 'backgroundColor', [1 1 1]); % 第三個UI控制物件,用以指定顯示曲面所用到的調色盤。 pos3 = [170, 10, 60, 20]; h3 = uicontrol('style', 'popupmenu', ... 'tag', 'UI3', ... 'string', 'hsv|gray|hot|cool|pink|jet', ... 'position', pos3); % 第一個UI控制物件的反應指令為「grid」。 set(h1, 'callback', 'grid'); % 第二個UI控制物件的反應指令為「ui02('callback2')」。 set(h2, 'callback', 'ui02(''callback2'')'); % 第三個UI控制物件的反應指令為「ui02('callback3')」。 set(h3, 'callback', 'ui02(''callback3'')');

38 case 'callback2' % 第二個UI控制物件的callback。
h1 = findobj(0, 'tag', 'UI1'); h2 = findobj(0, 'tag', 'UI2'); % 取得第二個UI控制物件的數值。 pointNum = round(str2num(get(h2, 'string'))); % 若數字太大或太小,則設定為10。 if pointNum <= 1 | pointNum > 100, pointNum = 10; end set(h2, 'string', int2str(pointNum)); % 根據所得的數字,重畫peaks曲面。 [xx, yy, zz] = peaks(pointNum); surf(xx, yy, zz); axis tight; % 根據第一個UI控制物件,決定是否要畫格線。 if get(h1, 'value')==1, grid on; else grid off;

39 case 'callback3' % 第三個UI控制物件的callback。
h3 = findobj(0, 'tag', 'UI3'); % 根據第三個UI控制物件來決定使用的色盤矩陣。 switch get(h3, 'value') case 1 colormap(hsv); case 2 colormap(gray); case 3 colormap(hot); case 4 colormap(cool); case 5 colormap(pink); case 6 colormap(jet); otherwise disp('Unknown option'); end otherwise, error('Unknown action string!');

40 滑鼠事件 (Mouse Events) 滑鼠事件 (Mouse Events)
MATLAB 程式設計入門篇 滑鼠事件 (Mouse Events) 滑鼠事件 (Mouse Events) WindowButtonDownFcn:滑鼠按鈕按下時反應指令 WindowButtonMotionFcn:滑鼠移動時的反應指令 WindowButtonUpFcn:滑鼠按鈕釋放時的反應指令 Ex:希望滑鼠先被按下後,再移動滑鼠時,才開始移動的反應指令: 滑鼠按鈕被按下時 設定WindowButtonMotionFcn 及WindowButtonDownFcn的值。 滑鼠按鈕被釋放時 清除WindowButtonMotionFcn 及WindowButtonUpFcn的值。

41 MATLAB 程式設計入門篇 滑鼠事件的例子 Mouse01.m, mouse02.m

42 Mouse01.m function mouse01(action) % mouse01: 本例展示如何設定滑鼠事件的反應指令
MATLAB 程式設計入門篇 Mouse01.m function mouse01(action) % mouse01: 本例展示如何設定滑鼠事件的反應指令 if nargin==0, action='start'; end switch(action) case 'start' % 開啟圖形視窗 axis([ ]); % 設定圖軸範圍 box on; % 將圖軸加上圖框 title('Click and drag your mouse in this window!'); % 設定滑鼠按鈕被按下時的反應指令為「mouse01 down」 set(gcf, 'WindowButtonDownFcn', 'mouse01 down'); case 'down' % 滑鼠按鈕被按下時的反應指令 % 設定滑鼠移動時的反應指令為「mouse01 move」 set(gcf, 'WindowButtonMotionFcn', 'mouse01 move'); % 設定滑鼠按鈕被釋放時的反應指令為「mouse01 up」 set(gcf, 'WindowButtonUpFcn', 'mouse01 up'); % 列印「Mouse down!」訊息 fprintf('Mouse down!\n');

43 currPt = get(gca, 'CurrentPoint'); x = currPt(1,1); y = currPt(1,2);
MATLAB 程式設計入門篇 case 'move' % 滑鼠移動時的反應指令 currPt = get(gca, 'CurrentPoint'); x = currPt(1,1); y = currPt(1,2); line(x, y, 'marker', '.', 'EraseMode', 'xor'); % 列印「Mouse is moving!」訊息及滑鼠現在位置 fprintf('Mouse is moving! Current location = (%g, %g)\n', ... currPt(1,1), currPt(1,2)); case 'up' % 滑鼠按鈕被釋放時的反應指令 % 清除滑鼠移動時的反應指令 set(gcf, 'WindowButtonMotionFcn', ''); % 清除滑鼠按鈕被釋放時的反應指令 set(gcf, 'WindowButtonUpFcn', ''); % 列印「Mouse up!」訊息 fprintf('Mouse up!\n'); end

44 Mouse02.m 執行結果與Mouse01.m比較 只有X,Y軸範圍改變 其他幾乎完全一樣 但是程式的寫法更為結構化
MATLAB 程式設計入門篇 Mouse02.m 執行結果與Mouse01.m比較 只有X,Y軸範圍改變 其他幾乎完全一樣 但是程式的寫法更為結構化

45 Mouse02.m function mouse02(action) % mouse02: 本例展示如何設定滑鼠事件的反應指令
if nargin==0, action='start'; end switch(action) % 開啟圖形視窗 case 'start' axis([ ]); % 設定圖軸範圍 box on; % 將圖軸加上圖框 title('Click and drag your mouse in this window!'); % 設定滑鼠按鈕被按下時的反應指令 set(gcf, 'WindowButtonDownFcn', sprintf('%s %s', mfilename, 'down')); case 'down' % 滑鼠按鈕被按下時的反應指令 % 設定滑鼠移動時的反應指令 set(gcf, 'WindowButtonMotionFcn', sprintf('%s %s', mfilename, 'move')); % 設定滑鼠按鈕被釋放時的反應指令為 set(gcf, 'WindowButtonUpFcn', sprintf('%s %s', mfilename, 'up')); % 列印「Mouse down!」訊息 fprintf('Mouse down!\n');

46 case 'move' % 滑鼠移動時的反應指令 fprintf('Mouse is moving! '); feval(mfilename, 'print'); % 列印「Mouse is moving!」訊息及滑鼠現在位置 case 'up' % 滑鼠按鈕被釋放時的反應指令 % 清除滑鼠移動時的反應指令 set(gcf, 'WindowButtonMotionFcn', ''); % 清除滑鼠按鈕被釋放時的反應指令 set(gcf, 'WindowButtonUpFcn', ''); % 列印「Mouse up!」訊息 fprintf('Mouse up!\n'); case 'print' currPt = get(gca, 'CurrentPoint'); x = currPt(1,1); y = currPt(1,2); line(x, y, 'marker', '.', 'EraseMode', 'xor'); fprintf('Current location = (%g, %g)\n', currPt(1,1), currPt(1,2)); end

47 showNearestPoint1.m function showNearestPoint1(action)
MATLAB 程式設計入門篇 showNearestPoint1.m function showNearestPoint1(action) global h0 h1 h2 h3 h4 x1 y1 if nargin==0 action='b'; end x1=linspace(0,4*pi,30); y1=sin(x1); plot(x1,y1,'r-o') axis equal

48 showNearestPoint1.m (Continue)
MATLAB 程式設計入門篇 showNearestPoint1.m (Continue) switch(action) case 'b' set(gcf,'windowbuttonmotionfcn','showNearestPoint1 a') case 'a' currPt=get(gca,'CurrentPoint'); x2=currPt(1,1); y2=currPt(1,2); Minx=sqrt((x2-x1).^2+(y2-y1).^2); n=find(Minx==min(Minx)); line(x1(n),y1(n),'marker','o') loction=[num2str(x1(n)) ',' num2str(y1(n))]; %text(x1(n),y1(n),loction);%顯示在點上 text(x2+.2,y2,loction);%顯示在游標旁 end

49 Basic GUI components

50

51 Uimenu01.m figure(1) f = uimenu('Label','Workspace');
uimenu(f,'Label','New Figure','Callback','figure'); uimenu(f,'Label','Save','Callback','save'); uimenu(f,'Label','Quit','Callback','exit',... 'Separator','on','Accelerator','Q'); figure('tag','uimenu','Resize','on','MenuBar','none',... 'Name','Teaching uimenu','NumberTitle','off','Position',[120,60,520,420]);

52

53 Uimenu02.m function uimenu02 ah1=findobj(0,'tag','SOLAR');close(ah1);
figure('tag','SOLAR','Resize','on','MenuBar','none',... 'Name','Solar Engineering Fundamentals','NumberTitle','off','Position',[120,60,520,420]); fp0 = uimenu('Label','Edit'); uimenu(fp0,'Label','Select Julian day','Callback','solar0(''edit'')'); uimenu(fp0,'Label','Select hour and angle for rt','Callback','solar0(''edit1'')'); fp1 = uimenu('Label','Radiation'); uimenu(fp1,'Label','ET radiation on surface Normal to the Sun','Callback','solar0(''ET Radiation'')','Accelerator','N'); uimenu(fp1,'Label','ET solar Irradiance', 'Callback','solar0(''ET SolarIrradiance'')','Accelerator','I'); uimenu(fp1,'Label','ET daily insolation on Hori. surface','Callback','solar0(''ET Rad_Horizontal'')','Accelerator','H'); uimenu(fp1,'Label','ET solar Fraction','Callback','solar0(''ET SolarFraction'')','Separator','on','Accelerator','F'); uimenu(fp1,'Label','Unit Conversion', 'Callback','solar0(''Transfer'')','Accelerator','C');

54

55 Uimenu02.m (continue) fp2 = uimenu('Label','Time');
uimenu(fp2,'Label','Solar decLination','Callback','solar0(''declination'')','Accelerator','L'); uimenu(fp2,'Label','Equation Of time','Callback','solar0(''eqoftime'')','Accelerator','O'); uimenu(fp2,'Label','The analeMma','Callback','solar0(''Analemma'')','Separator','on','Accelerator','M'); uimenu(fp2,'Label','Animated Analemma','Callback','Analemma','Accelerator','A'); uimenu(fp2,'Label','Analemma Photo','Callback','solar0(''Analemma_photo'')','Accelerator','P','Separator','on'); fp3 = uimenu('Label','Angle'); uimenu(fp3,'Label','Sunrise/sunset hour','Callback','solar0(''sunrise_sunset'')'); uimenu(fp3,'Label','Sun-Surface Angle 1','Callback','solar0(''Sun-SurfaceAngle'')','Separator','on'); uimenu(fp3,'Label','Sun-Surface Angle 2','Callback','Sc_cal'); uimenu(fp3,'Label','Solar Altitute vs. Azimuth Angle','Callback','solar0(''AlvsAz'')','Separator','on'); uimenu(fp3,'Label','Top view of Sun''s track','Callback','solar0(''topview'')'); uimenu(fp3,'Label','Side view of Sun''s track','Callback','solar0(''sideview'')'); uimenu(fp3,'Label','Global view of Sun''s track','Callback','solar0(''globalview'')');

56

57 Uimenu02.m (continue) fp4 = uimenu('Label','Terrestrial');
uimenu(fp4,'Label','ET total radiation ratio(rt) on hori. surface','Callback','solar0(''run_rt'')','Accelerator','T'); uimenu(fp4,'Label','ET diffuse radiation ratio(rd) on hori. surface','Callback','solar0(''ratio_rd'')','Accelerator','D'); uimenu(fp4,'Label','ET Hourly ratio', 'Callback','solar0(''Rt&Rd'')','Separator','on','Accelerator','O'); fh = uimenu('Label','Misc.'); uimenu(fh,'Label','Save current Julian day as default','Callback','solar0(''savedefault'')'); uimenu(fh,'Label','Reset Julian day','Callback','solar0(''reset'')'); uimenu(fh,'Label','Close All','Callback','solar0(''cbAct1'')','Separator','on'); uimenu(fh,'Label','Close','Callback','close'); uimenu(fh,'Label','About Author','Callback','solar0(''author'')','Separator','on'); uimenu(fh,'Label','About Software','Callback','solar0(''version'')');

58

59 Uipanel01.m h = figure; hp = uipanel('Title','Main Panel','FontSize',12,... 'BackgroundColor','white',... 'Position',[ ]); hsp1 = uipanel('Parent',hp,'Title','Subpanel_1','FontSize',12,... 'Position',[ ]); hsp2 = uipanel('Parent',hp,'Title','Subpanel_2','FontSize',12,... 'Position',[ ]); hbsp1 = uicontrol('Parent',hsp1,'String','Push here',... 'Position',[ ]); hbsp2 = uicontrol('Parent',hsp1,'String','Also here',... 'Position',[ ]);

60

61 Uibuttongroup01.m function uibuttongroup01
h = uibuttongroup('visible','off','Position',[ ]); leng=100; width=25; clc u0 = uicontrol('Style','Radio','String','Option 1',... 'pos',[ leng width],'parent',h,'HandleVisibility','off'); u1 = uicontrol('Style','Radio','String','Option 2',... 'pos',[ leng width],'parent',h,'HandleVisibility','off'); u2 = uicontrol('Style','Radio','String','Option 3',... 'pos',[ leng width],'parent',h,'HandleVisibility','off'); set(h,'SelectedObject',[]); % No selection set(h,'Visible','on'); function selcbk(source,eventdata) %disp(source); % show the handle disp([eventdata.EventName,' ',... get(eventdata.OldValue,'String'),' --> ', ... get(eventdata.NewValue,'String')]); disp(get(get(source,'SelectedObject'),'String

62

63 Uicontextmenu.m % Define the context menu cmenu = uicontextmenu;
% Define the line and associate it with the context menu hline = plot(1:10, 'UIContextMenu', cmenu); % Define callbacks for context menu items cb1 = ['set(hline, ''LineStyle'', ''--'')']; cb2 = ['set(hline, ''LineStyle'', '':'')']; cb3 = ['set(hline, ''LineStyle'', ''-'')']; % Define the context menu items item1 = uimenu(cmenu, 'Label', 'dashed', 'Callback', cb1); item2 = uimenu(cmenu, 'Label', 'dotted', 'Callback', cb2); item3 = uimenu(cmenu, 'Label', 'solid', 'Callback', cb3); % When the user extend-clicks on the line, the context menu appears.

64

65 Waitbar01.m h = waitbar(0,'Please wait...'); for i=1:100, % computation here % waitbar(i/100) end close(h)

66 Dialog Box

67 Errordlg01.m errordlg('File not found','File Error');

68 Helpdlg01.m helpdlg('Choose 10 points from the figure','Point Selection');

69 Warndlg01.m warndlg('Pressing OK will clear memory','!! Warning !!')

70 Listdlg01.m d = dir; str = {d.name}; [s,v] = listdlg('PromptString','Select a file:',... 'SelectionMode','single',... 'ListString',str)

71 Printdlg01.m printdlg printdlg(fig) printdlg('-crossplatform',fig) printdlg('-setup',fig)

72 Inputdlg01.m function inputdlg01 prompt='Enter your Department name';
answer = inputdlg(prompt); % answer is a cell array dlg_title=answer{1}; prompt='Enter your name'; answer = inputdlg(prompt,dlg_title); dlg_title=[dlg_title,' ',answer{1}]; num_lines=3; answer = inputdlg(prompt,dlg_title,num_lines); defAns={'方煒'}; % defAns specifies the default value to display for each prompt. % defAns must contain the same number of elements as prompt and all elements must be strings. answer = inputdlg(prompt,dlg_title,num_lines,defAns); defAns=answer; dlg_title=[dlg_title,' ',answer{1},' Resize is on']; Resize='on'; answer = inputdlg(prompt,dlg_title,num_lines,defAns,Resize);

73

74 questdlg01.m ButtonName=questdlg('What is your wish?', ...
'Genie Question', ... 'Food','Clothing','Money','Money'); switch ButtonName, case 'Food', disp('Food is delivered'); case 'Clothing', disp('The Emperor''s new clothes have arrived.') case 'Money', disp('A ton of money falls out the sky.'); end % switch

75

76 Uigetfile01.m function uigetfile01 [FileName,PathName] = uigetfile('*.m','Select the M-file');

77 Uigetfile02.m function uigetfile02 [filename, pathname] = ... uigetfile({'*.m';'*.mdl';'*.mat';'*.*'},'File Selector');

78 Uigetfile03.m function uigetfile03 [filename, pathname] = uigetfile( ... {'*.m;*.fig;*.mat;*.mdl','MATLAB Files (*.m,*.fig,*.mat,*.mdl)'; '*.m', 'M-files (*.m)'; ... '*.fig','Figures (*.fig)'; ... '*.mat','MAT-files (*.mat)'; ... '*.mdl','Models (*.mdl)'; ... '*.*', 'All Files (*.*)'}, ... 'Pick a file');

79 Uigetfile04.m function uigetfile04 [filename, pathname] = uigetfile('*.m', 'Pick an M-file'); if isequal(filename,0) disp('User selected Cancel') else disp(['User selected', fullfile(pathname, filename)]) end

80 Uigetfile05.m function uigetfile05 % allow for multi_select [filename, pathname, filterindex] = uigetfile( ... { '*.mat','MAT-files (*.mat)'; ... '*.mdl','Models (*.mdl)'; ... '*.*', 'All Files (*.*)'}, ... 'Pick a file', ... 'MultiSelect', 'on');

81 Uigetdir01.m dname = uigetdir('C:\');

82 Uigetdir02.m uigetdir(matlabroot,'MATLAB Root Directory‘)

83 Uiputfile01.m [file,path] = uiputfile('animinit.m','Save file name');

84 Uiputfile02.m [file,path] = uiputfile('*.mat','Save Workspace As');

85 Uiputfile03.m [filename, pathname] = uiputfile( ... {'*.m';'*.mdl';'*.mat';'*.*'}, ... 'Save as');

86 Uiputfile04.m [filename, pathname, filterindex] = uiputfile( ... {'*.m;*.fig;*.mat;*.mdl','MATLAB Files (*.m,*.fig,*.mat,*.mdl)'; '*.m', 'M-files (*.m)'; ... '*.fig','Figures (*.fig)'; ... '*.mat','MAT-files (*.mat)'; ... '*.mdl','Models (*.mdl)'; ... '*.*', 'All Files (*.*)'}, ... 'Save as');

87 Uiputfile05.m [filename, pathname] = uigetfile('*.m', 'Pick an M-file'); if isequal(filename,0) | isequal(pathname,0) disp('User selected Cancel') else disp(['User selected',fullfile(pathname,filename)]) end

88 Uisetcolor01.m h_or_c=[1 0 0]; c = uisetcolor(h_or_c, 'DialogTitle') h_or_c=[0 1 0]; h_or_c=[0 0 1];

89 Uisetfont01.m h = text(.5,.5,'Figure Annotation'); uisetfont(h,'Update Font') c1 = uicontrol('Style', 'pushbutton', ... 'Position', [ ], 'String', 'ABC'); % Create push button with string XYZ c2 = uicontrol('Style', 'pushbutton', ... 'Position', [ ], 'String', 'XYZ'); % Display set font dialog box for c1, make selections, save to d d = uisetfont(c1); % Apply those settings to c2 set(c2, d)

90

91 More of ui_functions uisave uiopen uitoolbar uitoggletool uipushtool

92 Some Figure Properties

93 Some Figure Properties

94 Some Uicontrol Properties

95 Some Uicontrol Properties

96

97

98 Uicontextmenu properties


Download ppt "MATLAB 程式設計入門篇 方煒 台大生機系"

Similar presentations


Ads by Google