[心得] uicontrol 元件使用小範例已回收

看板MATLAB作者 (源)時間16年前 (2009/06/25 14:28), 編輯推噓3(301)
留言4則, 4人參與, 最新討論串1/1
最近將uicontrol的元件大概玩過一輪,寫了一隻小程式分享給大家。 % 使用Script設計GUI介面 % 1. 以script寫callback的規則: % a. 程式要寫在中括號內,[ 開頭,] 結尾。 % b. 兩個單引號('')取代單引號(')。 % c. 程式換行使用單引號與連續符號('...),第二行以單引號開始編輯。 % 2. ui系列具有物件導向特性,並具有繼承的特性。 % 3. get指令可查詢物件屬性,set指令可設定物件屬性。 % 4. 輸入User Interface Objects相關元件指令 (uicontrol, uipanel, uitable, ...) % 若使用者沒有建立figure放置元件,matlab會自動建立一個。 % 5. Matlab GUI設計需要一步一步設定各元件的位置與屬性,在面版上 % 的設定可以採用help中GUI範例做好的面版來當作修改範本,或是自己 % 做一個範本,在之後的處理可將重心放在function的撰寫上。 % 清除變數 clear; close all; clc; % 確認現在的螢幕尺寸 myScreenSize = get(0,'ScreenSize'); % 建立一個figure來放後續的控制元件,當成from來使用,後面的參數可調整frame大小 。 % uicontrol, uipanel, myFig = figure('Name','使用Script設計GUI介面', 'Position',[30 myScreenSize(4)/3 640 400]); % 關掉圖形標號與menubar,gcf可以換成myFig set(gcf,'numbertitle','off','menubar','none') % 先定義好Callback,要放在被呼叫端的前面,編輯規則如1.所述 myCallback= ['get(myPop,''Value''), '... 'get(myEdit, ''String''), '.... 'set(myPanel,''Title'', get(myPop,''Value''))']; % ---------- 以下為uicontrol元件,共有10種 ---------- % % 圖層順序依照物件產生順序,所以先建立frame % % 編號依照help uicontrol 排列順序 % % c. 在myFig上放一個frame,frame上無法直接加label。 myFrame = uicontrol('Style', 'frame',... 'BackgroundColor',[0.50 0.50 0.50],... 'Position', [10 20 120 365]); % a. 建立一個check box,如果被點選value = 1,否則 = 0 myChkBox = uicontrol('Style', 'Checkbox', ... 'String', 'Check Box', ... 'Position', [20 350 100 25], ... 'Callback', 'helpwin uicontrol'); % b. 建立一個Editable text fields,接受myPop傳來的資料。 % 按enter會呼叫callback並主動抓取myPop的選擇結果 % 繼承myPanel, 單位使用unit時可依照parent的座標比率 % 進行Position配置,預設為pixels,座標定位點是以左下 % 角為原點。 myEdit = uicontrol('Style', 'edit',... 'String', 'Editable text fields',... 'Position', [20 310 100 25],... 'Callback', 'set(myEdit, ''String'',get(myPop,''Value''))'); % d. 建立一個listbox myList = uicontrol('style', 'Listbox', 'String', 'List boxes |this |is |Pop-up |List boxses', ... 'Position', [20 270 100 25]); % e. 建立一個Pop-up menus,當改變選項時呼叫myCallback。 % 如果是要引用已經定義好的函數,不需要加單引號 myPop = uicontrol('Style', 'popup',... 'String', 'Pop-up menus |this |is |Pop-up |menus',... 'Position', [20 230 100 25],... 'Callback', myCallback); % f. 建立一個pushbutton,如果沒有要get或set,元件可以不要命名。 uicontrol('Position', [20 190 100 30] ,... 'String', 'Pushbutton', ... 'Callback','figure(''Name'',''這是新建立的圖'', ''numbertitle'',''off''); x=linspace(0,10,1001); plot(x,sin(2*pi*x))'); % g. 建立一個radiobutton uicontrol('style', 'radiobutton', ... 'String', 'Radiobutton', ... 'Position', [20 150 100 30]); % h. 建立一個Sliders uicontrol('style', 'slider', ... 'Position', [20 110 100 30]); % i. 建立一個Static text labels uicontrol('style', 'text', ... 'String', 'Static text labels', ... 'Position', [20 70 100 30]); % j. 建立一個Togglebutton uicontrol('style', 'togglebutton', ... 'String', 'Togglebutton', ... 'Position', [20 30 100 30]); % ---------- 以下為uipanel元件 ---------- % % a. 在from上放一個panel,panel位置可以依據myFig的比率進行設定 myPanel = uipanel('Title','My panel','FontSize',12,... 'Position',[.22 .05 .75 .94]); % b. 在panel上放一個editable text fields,繼承 myPanel myEdit = uicontrol('Parent', myPanel,'Style', 'edit',... 'String', '這是繼承 Panel 的Editable text fields',... 'Units', 'normalized', ... 'Position', [.02 .75 .95 .2],... 'Callback', 'set(myEdit, ''String'',get(myPop,''Value''))'); % ---------- 以下為uicontextmenu元件(繼承 myPanel) ---------- % % 定義要畫圖的位置,參考help uicontextmenu % axes 的 parent 只能夠是 figure或是 uipanel hax = axes( 'Parent', myPanel, 'Units','normalized', 'Position',[0.05 0.5 0.9 0.20]); % Plot three lines plot(rand(20,3)); % Define a context menu; it is not attached to anything hcmenu = uicontextmenu; % Define callbacks for context menu items that change linestyle hcb1 = ['set(gco, ''LineStyle'', ''--'')']; hcb2 = ['set(gco, ''LineStyle'', '':'')']; hcb3 = ['set(gco, ''LineStyle'', ''-'')']; % Define the context menu items and install their callbacks item1 = uimenu(hcmenu, 'Label', 'dashed', 'Callback', hcb1); item2 = uimenu(hcmenu, 'Label', 'dotted', 'Callback', hcb2); item3 = uimenu(hcmenu, 'Label', 'solid', 'Callback', hcb3); % Locate line objects hlines = findall(hax,'Type','line'); % Attach the context menu to each line for line = 1:length(hlines) set(hlines(line),'uicontextmenu',hcmenu) end % ---------- 以下為uibuttongroup元件(繼承myPanel) ---------- % % Create the button group. h = uibuttongroup('Position',[0 0 1 .1], 'Parent' , myPanel ); % Create three radio buttons in the button group. u0 = uicontrol('Style','Radio','String','uibuttongroup 1', 'Units', 'normalized',... 'pos',[.05 .3 .2 .5],'parent',h,'HandleVisibility','off'); u1 = uicontrol('Style','Radio','String','uibuttongroup 2','Units', 'normalized',... 'pos',[.4 .3 .2 .5],'parent',h,'HandleVisibility','off'); u2 = uicontrol('Style','Radio','String','uibuttongroup 3','Units', 'normalized',... 'pos',[.7 .3 .2 .5],'parent',h,'HandleVisibility','off'); % ---------- uitoolbar, uitoggletool 與 uipushtool 元件 ---------- % % 要先具有uitoolbar元件才可以使用uitoggletool與 uipushtool % myToolbar = uitoolbar(myFig); a = [.20:.05:0.95]; img1(:,:,1) = repmat(a,16,1)'; img1(:,:,2) = repmat(a,16,1); img1(:,:,3) = repmat(flipdim(a,2),16,1); myTogglet = uitoggletool(myToolbar,'CData',img1,'TooltipString','toggletool'); uitoggletool(myToolbar, 'CData',rand(16,16,3),'Separator','on', 'TooltipString','Your toggle tool', 'HandleVisibility','off') uipushtool(myToolbar,'CData',img1,'Separator','on', 'TooltipString','uipushtool') % ---------- uitable (繼承myPanel)---------- % dat = rand(8,5); cnames = {'uitable','W-Data','X-Data','Y-Data','Z-Data'}; t = uitable('Data',dat,'ColumnName',cnames, 'Units', 'normalized',.... 'Parent',myPanel,'Position',[.02 .12 .95 .3]); -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.235

06/25 17:43, , 1F
感謝分享
06/25 17:43, 1F

06/25 21:53, , 2F
UI耶!! 推啦
06/25 21:53, 2F

06/25 22:43, , 3F
感謝分享~
06/25 22:43, 3F

07/01 20:40, , 4F
07/01 20:40, 4F
文章代碼(AID): #1AGnadM2 (MATLAB)
文章代碼(AID): #1AGnadM2 (MATLAB)