2009-07-12 36 views
8

我需要在MATLAB中爲我的項目創建一個GUI。我到處尋找如何編程GUI的例子,但我找不到很多。 MATLAB中的GUI編程有哪些好的站點或技術?如何在MATLAB中編程GUI?

回答

2

我最近有編程控制一些情節簡單的GUI的所有視頻。我不確切知道你的任務是什麼,但這裏有一些基本的代碼讓你開始。這創造了兩個數字;圖1有對照,圖2有一個y = x^p的圖。您在框中輸入p的值,然後按Enter鍵註冊並重新繪製;然後按按鈕重置爲默認值p = 1。

function SampleGUI() 
    x=linspace(-2,2,100); 
    power=1; 
    y=x.^power; 
    ctrl_fh = figure; % controls figure handle 
    plot_fh = figure; % plot figure handle 
    plot(x,y); 
    % uicontrol handles: 
    hPwr = uicontrol('Style','edit','Parent',... 
         ctrl_fh,... 
         'Position',[45 100 100 20],... 
         'String',num2str(power),... 
         'CallBack',@pwrHandler); 

    hButton = uicontrol('Style','pushbutton','Parent',ctrl_fh,... 
         'Position',[45 150 100 20],... 
         'String','Reset','Callback',@reset); 

    function reset(source,event,handles,varargin) % boilerplate argument string 
     fprintf('resetting...\n'); 
     power=1; 
     set(hPwr,'String',num2str(power)); 
     y=x.^power; 
     compute_and_draw_plot(); 
    end 

    function pwrHandler(source,event,handles,varargin) 
     power=str2num(get(hPwr,'string')); 
     fprintf('Setting power to %s\n',get(hPwr,'string')); 
     compute_and_draw_plot(); 
    end 

    function compute_and_draw_plot() 
     y=x.^power; 
     figure(plot_fh); plot(x,y); 
    end 
end 

GUI背後的基本思想是,當你操縱控件時,他們稱之爲「回調」函數,即事件處理程序;這些函數能夠通過使用控制手柄和set/get方法獲取或更改其屬性的控件進行交互。要獲取可用屬性列表,請仔細閱讀Matlab文檔網站(http://www.mathworks.com/access/helpdesk/help/techdoc/infotool/hgprop/doc_frame.html)上非常豐富的Handle圖形屬性瀏覽器;點擊UI對象(或其他任何你需要的)。

希望這會有所幫助!