簡體   English   中英

在matlab中創建未知數量的uicontrol

[英]Creating an unknown amount of uicontrols in matlab

因此,對於一個自我項目,我正在創建一個類似於matlab游戲的gui掃雷,並且想要創建一個可調節的按鈕網格,但我不知道如何這樣做。 這是我到目前為止所得到的。

function createField()

xAmount = str2double(inputdlg('enter row length'));
yAmount = str2double(inputdlg('enter column length'));

for i = 1:xAmount
    for j = 1:yAmount
    %create buttons
    end    
end
end

和大多數問題一樣,有許多不同的方法,寫了類似的東西,我會給你提供我在編寫輔助函數時使用的相同提示。


您的代碼將根據按下的按鈕執行操作,因此每個按鈕都需要其自己的唯一ID和屬性。 根據所使用的MATLAB版本,每個圖形元素都有一個句柄 從R2014b開始,圖形對象可以直接作為對象進行尋址,而不需要使用數字ID作為指針。

從圖形窗口開始,查看圖形屬性

h.mainfig = figure; % Arbitrary figure window
get(h.mainfig); % Dump properties to command window

現在我們可能最感興趣的是主圖窗口的UnitsPosition屬性,您可以在輔助函數中使用它來弄清楚如何在創建按鈕時調整大小和空間。

如果我們使用uicontrol()創建一個按鈕圖形對象,我們將獲得大多數相同的屬性

h.randombutton = uicontrol('Parent', h.mainfig, 'Style', 'pushbutton');
get(h.randombutton);

同樣,我們對UnitsPosition屬性感興趣。 我們也會對Callback屬性感興趣,這是我們與按鈕交互時執行的函數。 另一個好的是Tag屬性,可用於為每個按鈕設置唯一的字符串標記,以便與以后的邏輯一起使用。


您可能已經注意到我正在使用結構數組來存儲我的圖形句柄。 這類似於MATLAB在使用GUIDE創建GUI時生成其對象數據的方式,並且具有整齊的數據包傳遞我們的函數的巨大優勢。 關於結構數組的一個好處是你可以嵌套數據結構,允許我們輕松地生成和尋址圖形對象,而不需要聰明的動態字段引用eval() (yuck)。 我們可以這樣做,而不必像button_1button_2等那樣做。

h.button(1) = uicontrol('Parent', h.mainfig, 'Style', 'pushbutton');
h.button(2) = uicontrol('Parent', h.mainfig, 'Style', 'pushbutton');
...
h.button(n) = uicontrol('Parent', h.mainfig, 'Style', 'pushbutton');

現在我們知道如何以編程方式生成任意數量的按鈕,並在以后輕松解決它們。


除了按鈕生成,我們還有另一個我之前提到的關鍵功能,即按鈕回調。 回調函數遵循稍微不同的語法 ,因為它們本身傳遞兩個參數,即執行回調的對象的句柄和事件數據結構(有關更多信息,請參閱文檔)。 因為函數知道UI對象調用它,所以我們可以創建一個非常通用的函數。

希望這有幫助!

一種解決方案可能是:

function create_field(hparent, nx, ny, width, padding)

        % Test arguments
        if ~test_parent_handle(hparent)
                error('Parent must be a single valid graphic handle.');
        elseif ~test_positive_integer(nx)
                error('Number of buttons on X direction must be a scalar positive integer.');
        elseif ~test_positive_integer(ny)
                error('Number of buttons on Y direction must be a scalar positive integer.');
        elseif ~test_positive_integer(width) ...
        || (width >= 100)
                error('Button width must be a scalar positive integer smaller than 100.');
        elseif ~test_positive_integer(padding) ...
        || (padding >= 20)
                error('Button padding must be a scalar positive integer smaller than 20.');
        end;

        % Resize the parent to fit the button grid
        set(hparent, 'Units', 'pixels');
        ppos = get(hparent, 'Position');
        ppos(3) = nx*width + (nx-1)*padding;
        ppos(4) = ny*width + (ny-1)*padding;
        set(hparent, 'Position', ppos);

        % Create button grid
        for p = 1:nx
                for q = 1:ny
                        bpos = [                  % Button spec:
                           (p-1)*(width+padding)  %  - X
                           (q-1)*(width+padding)  %  - Y
                           width                  %  - W
                           width                  %  - H
                        ];
                        uicontrol(                              ...
                           'Units',     'pixels',               ...
                           'Tag',       sprintf('X%dY%d',p,q),  ...
                           'Style',     'pushbutton',           ...
                           'Parent',    hparent,                ...
                           'Position',  bpos                    ...                   
                        );
                end;
        end;

        % ----- NESTED FUNCTIONS -----
        function tf = test_parent_handle(value)
                tf = isscalar(value) ...
                  && ishandle(value);
        end

        function tf = test_positive_integer(value)
                tf = isscalar(value) ...
                  && isreal(value) ...
                  && isfinite(value) ...
                  && (value > 0) ...
                  && (fix(value) == value);
        end
end

對於具有15 x 10方形按鈕的圖形,每個按鈕的邊長為25像素,按鈕之間的填充為3像素,請調用:

create_field(figure(), 15, 10, 20, 3);

您是否考慮過在Java中創建ui - 未記錄的matlab有一些例子 Java將為您提供很好的LayoutManagers ,它將負責調整大小等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM